Java 未输入任何内容时如何在edittext中将默认值设置为0?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18476266/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:58:00  来源:igfitidea点击:

How to set default value to 0 in edittext when nothing is inputted?

javaandroid

提问by Kerv

I have an app here which adds the number. I have 4 edittexts here. What I want to happen is that when i entered none in one of the edittexts, it will assume that I entered 0. How can it be done? Here is my code:

我在这里有一个应用程序可以添加数字。我这里有 4 个编辑文本。我想要发生的是,当我在其中一个编辑文本中输入 none 时,它​​会假设我输入了 0。怎么做?这是我的代码:

public class Order extends Activity {
Button GoBackHome;
private Button button1;  
private EditText txtbox1,txtbox2,txtbox3,txtbox4;  
private TextView tv;
Button PayNow;

@Override
public void onBackPressed() {
}

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

        GoBackHome = (Button) findViewById(R.id.gohomebutton);
        PayNow = (Button) findViewById(R.id.button2);
        txtbox1= (EditText) findViewById(R.id.editText1);  
        button1 = (Button) findViewById(R.id.button1);  
        tv = (TextView) findViewById(R.id.editText5);  
        txtbox2= (EditText) findViewById(R.id.editText2);
        txtbox3= (EditText) findViewById(R.id.editText3);
        txtbox4= (EditText) findViewById(R.id.editText4);
        button1.setOnClickListener(new clicker());  

        GoBackHome.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final Intent i = new Intent(Order.this, MainActivity.class);               
                startActivity(i);
            }
        });

        PayNow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                final Intent i = new Intent(Order.this, Payment.class);               
                startActivity(i);
            }
        });


}


class clicker implements Button.OnClickListener  
{  
    public void onClick(View v)  
    {  
        String a,b,c,d;  
        Integer vis;  
        a = txtbox1.getText().toString();  
        b = txtbox2.getText().toString();
        c = txtbox3.getText().toString();
        d = txtbox4.getText().toString();
        vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3+Integer.parseInt(c)*4+Integer.parseInt(d)*5;  
        tv.setText(vis.toString());  
    }  
}  

}

采纳答案by Andrew G

You can do as Tushar said or you can initialize the value in the XML. Something like

您可以按照 Tushar 所说的去做,也可以初始化 XML 中的值。就像是

<EditText 
    android:name="@+id/editText1"
    android:text="0"/>

FYI you might also find it cleaner to handle your button clicks on xml like:

仅供参考,您可能还会发现处理 xml 上的按钮点击更干净,例如:

<Button
    android:name="@+id/btn1"
    android:onClick="handleClicks"/>

and then in java you'd have a public void method:

然后在java中你会有一个public void方法:

public void handleClicks(View clickedView){
   if(clickedView.getId() == btn1.getId(){
       ...
   } else if (...){}
}

回答by Tushar

initialize as :

初始化为:

txtbox1.setText("0");

回答by Hugo Hideki Yamashita

You can set android:hint="0"in your XML file, then, in your code, you can check if it's empty (maybe using TextUtils.isEmpty()) and setting some variable to 0.

您可以android:hint="0"在您的 XML 文件中进行设置,然后在您的代码中,您可以检查它是否为空(可能使用TextUtils.isEmpty())并将某些变量设置为 0。

android:hint="0"will make a "0" appear in your EditTexts, but the "0" will disappear when anything is inputted.

android:hint="0"将使“0”出现在您的EditTexts 中,但输入任何内容时“0”都会消失。

Then you can change the onClick()to this:

然后您可以将其更改onClick()为:

class clicker implements Button.OnClickListener {  
    public void onClick(View v) {  
        String a,b,c,d;  
        Integer vis;  
        a = txtbox1.getText().toString();  
        b = txtbox2.getText().toString();
        c = txtbox3.getText().toString();
        d = txtbox4.getText().toString();

        try {
            vis = Integer.parseInt(a)*2+Integer.parseInt(b)*3+Integer.parseInt(c)*4+Integer.parseInt(d)*5;  
            tv.setText(vis.toString());
        } catch (NumberFormatException e) {
            vis = "0";
        }

        // Do something with "vis"
    }  
} 

Or you can create a method to check a value, try to parse to an int or return a default value.

或者您可以创建一个方法来检查一个值,尝试解析为一个 int 或返回一个默认值。

public int getInt(String edtValue, int defaultValue) {
    int value = defaultValue;

    if (edtValue != null) {
        try {
            value = Integer.parseInt(edtValue);
        } catch (NumberFormatException e) {
            value = defaultValue;
        }
    }

    return value;
}

Then you change your call to

然后你改变你的电话

vis = this.getInt(a, 0) * 2 + this.getInt(b, 0) * 3 + this.getInt(c, 0) * 4 + this.getInt(d, 0) * 5; 

回答by Rachit Mishra

Check the EditText length when you get it

得到 EditText 时检查它的长度

String value = null;

if(ed.getText().length()){
 value = textBox.getText().toString();
} else 
 value = 0+"";