繁体   English   中英

将空白的edittext值设置为零

[英]Set empty edittext value to zero

我创建了一个简单的程序来尝试弄清楚该如何做。 它具有两个编辑文本字段(输入类型数字十进制),一个文本视图和一个按钮。 我希望当用户单击按钮时在文本视图中显示两个输入字段的总和。 有人可以告诉我,如果用户将其保留为空白,如何将一个编辑文本字段的值设置为零? 我尝试了很多方法,但是没有任何效果。 编辑:我想实现这一点,同时保持与以前一样的编辑文本提示(而不将值更改为零,例如“ setText(” 0“);”。

这是我的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();


    }
}
}

您是否知道在Java中如果不使用默认值初始化变量,则默认值为0? 简而言之:

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


    }

如果您没有在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 + "");


}

您可以在oncreate方法中将编辑文本的值设置为0。 喜欢,

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


    }
}
}

它对您有帮助。

暂无
暂无

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

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