简体   繁体   中英

Percentage Calculator isn't Working in Android Studio

My application contains 3 EditText Properties

The first property takes the value of percentage from the user and second property should get the Number of what the user want to be calculate the percent for and the third property should Display the result.

I have attached the java and xml code below! Thank you.

I have a logic error. I'm expecting to get the value of a specific number with specific percentage the user had entered.

For eg. User wants 25% of 200 then in the first EditText, user will enter 25% and then in the second Edittext he will enter 200 and the value should be automatically displayed in the third Edittext ie 50. But here the Third Edittext is not updating its value and showing 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));
}

Assuming Percent is defined as a float, you should change the second to the last instruction to this:

Percent = (txt2 * txt) / 100.00;

If you use a literal number which is an Integer, such as 100, in an equation which also includes floats then the result may be cast as an Integer and rounding or truncation may occur.

I think your problem is caused by the fact that the user enters '25%' in textbox 1. When you try to parse that into a float, you will get the format error because the '%' is not a numeric value. You need to somehow convert the 25% to .25 before you try to parse that value into the float. Otherwise, it will generate the error and replace the first value as 0.0, which will then cause the Percent calculation to result in 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.
}

You can verify this by putting a breakpoint in your code on that line in the Exception, and proving that in fact, the exception is happening here.

如果Percent是您定义的浮点数,则应通过以下方法解决:

Percent = (txt2 * txt) / 100f;

First you need to assure that the first and the second EditText are filled and then change the value of the third EditText.
see https://developer.android.com/reference/android/text/TextWatcher
To do so you have to add an TextWatcher to both the first and the second EditText; lets call them edtPercentage, edtOriginalValue and 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]);
    }
});


do the same to the EditText edtOriginalValue so when you change either the percentage or the value to take the percentage the final EditText will update

In your code, the text of the EditText won't be updated since onCreate method will be called only when the application is launched. So need to add change listener on the EditText . Try the below code:

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));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM