Java 仅在EditText中验证数字
我试图做一个简单的功能,用户插入2个数字和一个祝酒词表示的总和。。。 首先,为了做到这一点,我想验证用户只插入数字。如果不是,我想设置toast消息(错误)说明这里只允许数字。我使用布尔isdigit(编辑文本输入)方法。 但是,我得到一个错误-可能是因为for循环不能接受nullJava 仅在EditText中验证数字,java,android,validation,Java,Android,Validation,我试图做一个简单的功能,用户插入2个数字和一个祝酒词表示的总和。。。 首先,为了做到这一点,我想验证用户只插入数字。如果不是,我想设置toast消息(错误)说明这里只允许数字。我使用布尔isdigit(编辑文本输入)方法。 但是,我得到一个错误-可能是因为for循环不能接受null public class MainActivity extends AppCompatActivity { private EditText firstIntCB, secundIntCB; private Butt
public class MainActivity extends AppCompatActivity {
private EditText firstIntCB, secundIntCB;
private Button getProductCB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addDataToDisplay();
}
public void addDataToDisplay() {
if (isdigit(firstIntCB)==true && isdigit(secundIntCB)==true){
firstIntCB = (EditText)findViewById(R.id.firstNumber);
secundIntCB = (EditText)findViewById(R.id.secundNumber);
getProductCB =(Button)findViewById(R.id.GetProduct);
getProductCB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer intFirst = Integer.parseInt(firstIntCB.getText().toString());
Integer intSec = Integer.parseInt(secundIntCB.getText().toString());
String product = Integer.toString((int)Math.pow(intFirst,intSec));
Toast.makeText
(getApplicationContext(),String.valueOf(product), Toast.LENGTH_LONG).show();
}
});
}
}
public boolean isdigit(EditText input) {
String data=input.getText().toString().trim();
for(int i=0;i<data.length();i++)
{
if (!Character.isDigit(data.charAt(i)))
return false;
}
return true;
}
public类MainActivity扩展了AppCompatActivity{
私人编辑文本firstIntCB,secundIntCB;
私有按钮getProductCB;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addDataToDisplay();
}
public void addDataToDisplay(){
if(isdigit(firstIntCB)==true&&isdigit(secundIntCB)==true){
firstIntCB=(EditText)findViewById(R.id.firstNumber);
secunditcb=(EditText)findViewById(R.id.secundNumber);
getProductCB=(按钮)findViewById(R.id.GetProduct);
getProductCB.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
整数intFirst=Integer.parseInt(firstIntCB.getText().toString());
Integer intSec=Integer.parseInt(secunditcb.getText().toString());
字符串乘积=Integer.toString((int)Math.pow(intFirst,intSec));
Toast.makeText
(getApplicationContext(),String.valueOf(product),Toast.LENGTH_LONG.show();
}
});
}
}
公共布尔值isdigit(编辑文本输入){
字符串数据=input.getText().toString().trim();
对于(int i=0;i尝试使用android:inputType=“number”
Editext
的属性,该属性只允许在Editext
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"/>
在
EditText
属性中添加digits
和inputType
属性将允许用户在EditText
属性中输入特定字符,因此无需添加过滤器
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:digits="0123456789"/>
你犯了一个错误。你应该在你的
onCreate
中findViewById
,然后做其他事情
编译器尝试解析if条件,但您未定义编辑文本
因此:
然后将android:inputType=“number”
放入编辑文本xml中
我希望这对您有用更改addDataToDisplay();方法如下
public void addDataToDisplay() {
firstIntCB = (EditText)findViewById(R.id.firstNumber);
secundIntCB = (EditText)findViewById(R.id.secundNumber);
getProductCB =(Button)findViewById(R.id.GetProduct);
getProductCB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isdigit(firstIntCB)==true && isdigit(secundIntCB)==true) {
Integer intFirst = Integer.parseInt(firstIntCB.getText().toString());
Integer intSec = Integer.parseInt(secundIntCB.getText().toString());
String product = Integer.toString((int) Math.pow(intFirst, intSec));
Toast.makeText
(getApplicationContext(), String.valueOf(product), Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"Enter values", Toast.LENGTH_LONG).show();
}
}
});
}
错误是什么?好的..因此我必须使用setOnClickListener来实时检查输入是否有效..否@RonenRachmani这是一个错误。请检查编辑的答案是的,我知道这个选项,但这是简单的方法..THX
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstIntCB = (EditText)findViewById(R.id.firstNumber);
secundIntCB = (EditText)findViewById(R.id.secundNumber);
getProductCB =(Button)findViewById(R.id.GetProduct);
addDataToDisplay();
}
`@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstIntCB = (EditText)findViewById(R.id.firstNumber);
secundIntCB = (EditText)findViewById(R.id.secundNumber);
getProductCB =(Button)findViewById(R.id.GetProduct);
addDataToDisplay();
}
public void addDataToDisplay() {
firstIntCB = (EditText)findViewById(R.id.firstNumber);
secundIntCB = (EditText)findViewById(R.id.secundNumber);
getProductCB =(Button)findViewById(R.id.GetProduct);
getProductCB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isdigit(firstIntCB)==true && isdigit(secundIntCB)==true) {
Integer intFirst = Integer.parseInt(firstIntCB.getText().toString());
Integer intSec = Integer.parseInt(secundIntCB.getText().toString());
String product = Integer.toString((int) Math.pow(intFirst, intSec));
Toast.makeText
(getApplicationContext(), String.valueOf(product), Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext(),"Enter values", Toast.LENGTH_LONG).show();
}
}
});
}