简体   繁体   中英

Set empty edittext value to zero

I have created a simple program to try to figure out how to do this. it has two edit text fields (input type number decimal), a text view and a button. I want the sum of the two input fields to be displayed in the text view when the user hits the button. can someone tell me how to set the value of one edit text field to zero if the user left it blank? I have tried many ways but nothing worked. Edit: i want to achieve this while keeping the hint of edit text as before (without changing the value to zero like " setText("0"); ".

here's my java code (tell me what to add)

package com.example.android.testedittexttozero;

import...

public class MainActivity extends AppCompatActivity {

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


public void calculate(View v) {   //the button
    final EditText number1 = (EditText) findViewById(R.id.numberFirst);
    EditText number2 = (EditText) findViewById(R.id.numberSecond);
    TextView total = (TextView) findViewById(R.id.totalTV);

    try {
        int a = Integer.parseInt(number1.getText().toString());
        int b = Integer.parseInt(number2.getText().toString());
        int sum = a + b;

        total.setText("" + sum);

    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "One or more field is empty", Toast.LENGTH_LONG).show();


    }
}
}

Do you know that in java if you dont initialize a variable with a default value, it's default value be 0? in short:

try {
        int a;//by default, it will be 0;
        int b = Integer.parseInt(number2.getText().toString());//let it be 2
        int sum = a + b;

        total.setText("" + sum);//Ans will be 2 only

    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "One or more field is empty", Toast.LENGTH_LONG).show();


    }

If you don't enter any value in a, it will be set to as 0. Like if you leave a blank, and enter 2 in second edittext, the ans will be two nevertheless..

     public void ZeroingEmpytEditText() {
    int f= 0;  // Zeroing Factor
    if (number1.getText().toString().length() > 0) {
        a = Integer.parseInt(number1.getText().toString());

    } else {
        a=f;

    }

    if (number2.getText().toString().length() > 0) {
        b = Integer.parseInt(number2.getText().toString());

    } else {
        b=f;
    }

    int sum = a + b ;
    total.setText(sum + "");


}

you can set value 0 of edit text in oncreate method. like,

public class MainActivity extends AppCompatActivity {
EditText number1,number2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    number1 = (EditText) findViewById(R.id.numberFirst);
    number2 = (EditText) findViewById(R.id.numberSecond);
    number1.settext("0");
    number2.settext("0");
}


public void calculate(View v) {   //the button

    TextView total = (TextView) findViewById(R.id.totalTV);

    try {
        int a = Integer.parseInt(number1.getText().toString());
        int b = Integer.parseInt(number2.getText().toString());
        int sum = a + b;

        total.setText("" + sum);

    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "One or more field is empty", Toast.LENGTH_LONG).show();


    }
}
}

its helpfull for you.

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