【问题标题】:Set empty edittext value to zero将空的edittext值设置为零
【发布时间】:2019-03-15 16:02:16
【问题描述】:

我创建了一个简单的程序来尝试弄清楚如何做到这一点。它有两个编辑文本字段(输入类型数字十进制)、一个文本视图和一个按钮。当用户点击按钮时,我希望两个输入字段的总和显示在文本视图中。如果用户将其留空,有人可以告诉我如何将一个编辑文本字段的值设置为零?我尝试了很多方法,但没有任何效果。 编辑:我想在保持编辑文本提示的同时实现这一点(不将值更改为零,如“ 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 android android-edittext


【解决方案1】:

你知道在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..

【讨论】:

  • 仅当您在类级别(字段)而不是在方法内部定义变量(在本例中为 a)时才适用
  • 绝对!为此+1!但我猜 ide 会要求你给出一些初始值,对吧?
  • 是的,它会在红色下划线并要求您对其进行初始化:)
  • 但是如果用户将number2字段留空,它仍然可以工作吗?
  • 执行上述代码会更好地回答这个问题
【解决方案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 + "");


}

【讨论】:

    【解决方案3】:

    您可以在 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();
    
    
        }
    }
    }
    

    对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-10
      • 1970-01-01
      • 1970-01-01
      • 2023-01-11
      • 1970-01-01
      • 1970-01-01
      • 2021-11-23
      • 2011-08-22
      相关资源
      最近更新 更多