Java 从 EditText Android 获取整数值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25804550/
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
get Integer value from EditText Android
提问by user4030877
This is my activity.xml
这是我的 activity.xml
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/inputnumber"
android:inputType="number" />
this is mainactivity.java
:
这是mainactivity.java
:
EditText num = (EditText) findViewById(R.Id.inputnumber) ;
the question is how can I get the integer value from this field as an integer and use it for example in a custom class I made like this to add 1 to the value of this input field?
问题是如何从该字段中获取整数值作为整数,并在例如我这样制作的自定义类中使用它来将 1 添加到该输入字段的值中?
my customclass.java
我的 customclass.java
public static int addone(int a)
{
int b = a+1;
return b;
}
采纳答案by ivan.mylyanyk
you can achieve this as
你可以做到这一点
int val = Integer.parseInt( num.getText().toString() );
and than pass val
to the method addone(val);
然后传递val
给方法addone(val);
回答by Sam
Get it beautiful
变漂亮
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (checkEmptyFileds(txt_birey_adet, txt_limit)) {
int girilenSayi = Integer.parseInt(txt_birey_adet.getText().toString());
int limit = Integer.parseInt(txt_limit.getText().toString());
}
}
});
private boolean checkEmptyFileds(EditText txt_birey_adet, EditText txt_limit) {
boolean onay = true;
if (txt_birey_adet.getText().toString().equals("")) {
// Util.getInstance(OzelHesap.this).bounceEffect3(txt_birey_adet);
onay = false;
} else if (txt_limit.getText().toString().equals("")) {
//Util.getInstance(OzelHesap.this).bounceEffect3(txt_limit);
onay = false;
}
return onay;
}