百分比计算器在 Android Studio 中不工作

Percentage Calculator isn't Working in Android Studio

我的应用程序包含 3 个 EditText 属性

第一个 属性 从用户那里获取百分比值,第二个 属性 应该获取用户想要计算百分比的数量,第三个 属性应该显示结果。

我在下面附上了 java 和 xml 代码!谢谢。

我有一个逻辑错误。我期望获得具有用户输入的特定百分比的特定数字的值。

例如。用户想要 200 的 25%,然后在第一个 EditText 中,用户将输入 25%,然后在第二个 Edittext 中他将输入 200,该值应该自动显示在第三个 Edittext 中,即 50。但是这里第三个 Edittext 没有更新它的值并显示 0.0.

    <EditText
    android:id="@+id/txtbx1"
    android:layout_width="179dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.462" />

    <EditText
    android:id="@+id/txtbx2"
    android:layout_width="179dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.585" />

    <EditText
    android:id="@+id/txtbx3"
    android:layout_width="179dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:ems="10"
    android:inputType="number"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.708" />

    <TextView
    android:id="@+id/txtvw1"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:text="%"
    android:textSize="30dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.144"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.462" />

    <TextView
    android:id="@+id/txtvw3"
    android:layout_width="wrap_content"
    android:layout_height="45dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:text="is"
    android:textSize="30dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.144"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.708" />

    <TextView
    android:id="@+id/txtvw2"
    android:layout_width="33dp"
    android:layout_height="45dp"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:text="of"
    android:textSize="30dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.133"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.585" />

    <TextView
    android:id="@+id/PC"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:fontFamily="@font/sansationbold"
    android:text="Percentage Calculator"
    android:textSize="30dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.139" />

    <TextView
    android:id="@+id/acl"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="320dp"
    android:layout_marginLeft="320dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginBottom="8dp"
    android:fontFamily="@font/casanova"
    android:text="Arcis Computer Labs"
    android:textSize="10dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.988" />

    setContentView(R.layout.activity_mainscreen);
    EditText txtbx1 = (EditText)findViewById(R.id.txtbx1);
    EditText txtbx2 = (EditText)findViewById(R.id.txtbx2);
    EditText txtbx3 = (EditText)findViewById(R.id.txtbx3);
    TextView txtvw1 = (TextView)findViewById(R.id.txtvw1);
    TextView txtvw2 = (TextView)findViewById(R.id.txtvw2);
    TextView txtvw3 = (TextView)findViewById(R.id.txtvw3);

    float txt;
    try{
        txt = Float.parseFloat(txtbx1.getText().toString());
    }
    catch(NumberFormatException x){
        txt = (float) 0.0;
    }
    float txt2;
    try{
        txt2 = Float.parseFloat(txtbx2.getText().toString());
    }
    catch(NumberFormatException ex){
        txt2 = (float) 0.0;
    }
    Percent = (txt2 * txt) / 100;
    txtbx3.setText(String.valueOf(Percent));
}

假设 Percent 定义为浮点数,您应该将倒数第二条指令更改为:

Percent = (txt2 * txt) / 100.00;

如果您在还包含浮点数的方程式中使用作为整数的文字数字,例如 100,则结果可能会转换为整数,并且可能会发生舍入或截断。

我认为您的问题是由用户在文本框 1 中输入“25%”引起的。当您尝试将其解析为浮点数时,您会收到格式错误,因为“%”不是数值。在尝试将该值解析为浮点数之前,您需要以某种方式将 25% 转换为 .25。否则,它将生成错误并将第一个值替换为 0.0,这将导致百分比计算结果为 0.0

float txt;
try{
    txt = Float.parseFloat(txtbx1.getText().toString()); // this will generate an error on a value of '25%'
}
catch(NumberFormatException x){
    txt = (float) 0.0; // so, this will execute.
}

您可以通过在异常的那一行代码中放置一个断点来验证这一点,并证明实际上异常发生在这里。

如果 Percent 是您定义的浮点数,则应按以下方法修复它:

Percent = (txt2 * txt) / 100f;

首先你需要确保第一个和第二个EditText被填充,然后改变第三个EditText的值。
https://developer.android.com/reference/android/text/TextWatcher
为此,您必须向第一个和第二个 EditText 添加一个 TextWatcher;让我们称它们为 edtPercentage、edtOriginalValue 和 edtFinalValue:

edtPercentage.addTextChangedListener(new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    //Here you can define code to happen before the change
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after{
    //Here you can define code to happen on change
    }

    @Override
    public void afterTextChanged(Editable s) {
    //Here you can define code to happen after the change

    if(edtPercentage.getText().length()>0 && edtOriginalValue.getText().length()>0)
    //edtFinalValue.setText([metod to do the calculation and parse to string]);
    }
});


对 EditText edtOriginalValue 执行相同操作,因此当您更改百分比或值以采用百分比时,最终 EditText 将更新

在您的代码中,EditText 的文本不会更新,因为 onCreate 方法只会在应用程序启动时调用。所以需要在 EditText 上添加变化监听器。试试下面的代码:

float txt1 = 0, txt2 = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText txtbx1 = (EditText) findViewById(R.id.txtbx1);
    EditText txtbx2 = (EditText) findViewById(R.id.txtbx2);
    final EditText txtbx3 = (EditText) findViewById(R.id.txtbx3);
    TextView txtvw1 = (TextView) findViewById(R.id.txtvw1);
    TextView txtvw2 = (TextView) findViewById(R.id.txtvw2);
    TextView txtvw3 = (TextView) findViewById(R.id.txtvw3);


    txtbx1.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() != 0)
                txt1 = Float.parseFloat(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {
            changeValue(txt1, txt2, txtbx3);
        }
    });

    txtbx2.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() != 0)
                txt2 = Float.parseFloat(s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {
            changeValue(txt1, txt2, txtbx3);
        }
    });


}

private void changeValue(float txt1, float txt2, EditText txtbx3) {
    float Percent = (txt2 * txt1) / 100;
    txtbx3.setText(String.valueOf(Percent));
}