android 我应该写什么条件才能让一切正常

ecbunoof  于 2023-03-16  发布在  Android
关注(0)|答案(1)|浏览(114)

我正在创建一个应用程序,它可以简单地计算我在任何情况下坐了多少小时。当你点击+1或-1时(尽管这不是特别必要:)),我应该会相应地显示一个数字。代码XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn11"
        android:layout_width="79dp"
        android:layout_height="44dp"
        android:layout_marginEnd="12dp"
        android:layout_marginBottom="180dp"
        android:text="+1"
        app:layout_constraintBottom_toTopOf="@+id/btn21"
        app:layout_constraintEnd_toStartOf="@+id/btn12"
        app:layout_constraintHorizontal_bias="0.448"
        app:layout_constraintStart_toEndOf="@+id/txtTime1" />

    <Button
        android:id="@+id/btn21"
        android:layout_width="79dp"
        android:layout_height="44dp"
        android:layout_marginEnd="28dp"
        android:layout_marginBottom="212dp"
        android:text="+1"
        app:layout_constraintBottom_toTopOf="@+id/btn31"
        app:layout_constraintEnd_toStartOf="@+id/btn22" />

    <Button
        android:id="@+id/btn31"
        android:layout_width="79dp"
        android:layout_height="44dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="60dp"
        android:text="+1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btn32" />

    <Button
        android:id="@+id/btn12"
        android:layout_width="79dp"
        android:layout_height="44dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="180dp"
        android:text="-1"
        app:layout_constraintBottom_toTopOf="@+id/btn22"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:id="@+id/btn22"
        android:layout_width="79dp"
        android:layout_height="44dp"
        android:layout_marginEnd="12dp"
        android:layout_marginBottom="212dp"
        android:text="-1"
        app:layout_constraintBottom_toTopOf="@+id/btn32"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:id="@+id/btn32"
        android:layout_width="79dp"
        android:layout_height="44dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="60dp"
        android:text="-1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <TextView
        android:id="@+id/txtJava"
        android:layout_width="188dp"
        android:layout_height="62dp"
        android:text="JAVA"
        android:textAlignment="center"
        android:textSize="40sp"
        app:layout_constraintBottom_toTopOf="@+id/txtTime1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.053"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.32" />

    <TextView
        android:id="@+id/txtEng"
        android:layout_width="188dp"
        android:layout_height="62dp"
        android:layout_marginBottom="24dp"
        android:text="ENGLISH"
        android:textAlignment="center"
        android:textSize="40sp"
        app:layout_constraintBottom_toTopOf="@+id/txtTime2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.062"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/txtMath"
        android:layout_width="188dp"
        android:layout_height="62dp"
        android:layout_marginBottom="24dp"
        android:text="MATH"
        android:textAlignment="center"
        android:textSize="40sp"
        app:layout_constraintBottom_toTopOf="@+id/txtTime3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.053"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/txtTime1"
        android:layout_width="188dp"
        android:layout_height="62dp"
        android:layout_marginStart="12dp"
        android:layout_marginBottom="80dp"
        android:text="TIME"
        android:textAlignment="center"
        android:textSize="40sp"
        app:layout_constraintBottom_toTopOf="@+id/txtEng"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/txtTime2"
        android:layout_width="188dp"
        android:layout_height="62dp"
        android:layout_marginStart="12dp"
        android:layout_marginBottom="104dp"
        android:text="TIME"
        android:textAlignment="center"
        android:textSize="40sp"
        app:layout_constraintBottom_toTopOf="@+id/txtMath"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/txtTime3"
        android:layout_width="188dp"
        android:layout_height="62dp"
        android:layout_marginStart="12dp"
        android:layout_marginBottom="52dp"
        android:text="TIME"
        android:textAlignment="center"
        android:textSize="40sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

JAVA代码:

package com.example.times;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView txtJava;
    private TextView txtTime1;

    private TextView txtEng;
    private TextView txtTime2;

    private TextView txtMath;
    private TextView txtTime3;

    private Button btn11;
    private Button btn12;
    private Button btn21;
    private Button btn22;
    private Button btn31;
    private Button btn32;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtJava = findViewById(R.id.txtJava);
        txtTime1 = findViewById(R.id.txtTime1);
        txtEng = findViewById(R.id.txtEng);
        txtTime2 = findViewById(R.id.txtTime2);
        txtMath = findViewById(R.id.txtMath);
        txtTime3 = findViewById(R.id.txtTime3);
        btn11 = findViewById(R.id.btn11);
        btn12 = findViewById(R.id.btn12);
        btn21 = findViewById(R.id.btn21);
        btn22 = findViewById(R.id.btn22);
        btn31 = findViewById(R.id.btn31);
        btn32 = findViewById(R.id.btn32);

        btn11.setOnClickListener(listener);
        btn12.setOnClickListener(listener);
        btn21.setOnClickListener(listener);
        btn22.setOnClickListener(listener);
        btn31.setOnClickListener(listener);
        btn32.setOnClickListener(listener);
    }

    private View.OnClickListener listener = new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            int value1 = 0;
            int value2 = 0;
            int value3 = 0;
            switch (view.getId()) {
                case R.id.btn11:
                    value1+=1;
                    String s11 = String.valueOf(value1);
                    txtTime1.setText(s11);
                    break;
                case R.id.btn12:
                    value1-=1;
                    String s12 = String.valueOf(value1);
                    txtTime1.setText(s12);
                    break;
                case R.id.btn21:
                    value2+=1;
                    String s21 = String.valueOf(value2);
                    txtTime2.setText(s21);
                    break;
                case R.id.btn22:
                    value2-=1;
                    String s22 = String.valueOf(value2);
                    txtTime2.setText(s22);
                    break;
                case R.id.btn31:
                    value3+=1;
                    String s31 = String.valueOf(value3);
                    txtTime3.setText(s31);
                    break;
                case R.id.btn32:
                    value3-=1;
                    String s32 = String.valueOf(value3);
                    txtTime3.setText(s32);
                    break;
            }

        }
    };
}

我使用case是因为我认为代码会更短,只是为了练习使用它。我基本上知道我的代码中有什么问题,但我不知道如何修复它。在这里,可能有必要规定一个条件,但我不明白是什么。循环在这里是无用的(我只是总是喜欢在错误的地方插入循环)。

jckbn6z7

jckbn6z71#

现在我理解了您的问题,我可以帮助您。在onClick方法中,您需要首先尝试解析txtTime1txtTime2txtTime3中的现有值。然后检查单击了哪个按钮。然后执行范围验证。最后将值设置回文本字段。类似于:

public void onClick(View view) {
    int value1 = 0;
    try {
        value1 = Integer.parseInt(txtTime1.getText());
    } catch (NumberFormatException nfe) {
    }
    int value2 = 0;
    try {
        value2 = Integer.parseInt(txtTime2.getText());
    } catch (NumberFormatException nfe) {
    }
    int value3 = 0;
    try {
        value3 = Integer.parseInt(txtTime3.getText());
    } catch (NumberFormatException nfe) {
    }
    switch (view.getId()) {
    case R.id.btn11:
        value1 += 1;
        break;
    case R.id.btn12:
        value1 -= 1;
        break;
    case R.id.btn21:
        value2 += 1;
        break;
    case R.id.btn22:
        value2 -= 1;
        break;
    case R.id.btn31:
        value3 += 1;
        break;
    case R.id.btn32:
        value3 -= 1;
        break;
    }
    value1 = Integer.min(100, value1);
    value1 = Integer.max(0, value1);
    value2 = Integer.min(100, value2);
    value2 = Integer.max(0, value2);
    value3 = Integer.min(100, value3);
    value3 = Integer.max(0, value3);
    txtTime1.setText(String.valueOf(value1));
    txtTime2.setText(String.valueOf(value2));
    txtTime3.setText(String.valueOf(value3));
}

相关问题