简体   繁体   English

将空白的edittext值设置为零

[英]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"); ". 编辑:我想实现这一点,同时保持与以前一样的编辑文本提示(而不将值更改为零,例如“ setText(” 0“);”。

here's my java code (tell me what to add) 这是我的Java代码(告诉我要添加的内容)

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? 您是否知道在Java中如果不使用默认值初始化变量,则默认值为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.. 如果您没有在a中输入任何值,它将被设置为0。就像您留空,然后在第二个edittext中输入2一样,ans仍然是2。

     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. 您可以在oncreate方法中将编辑文本的值设置为0。 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. 它对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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