我正在制作一个程序,您可以在两个不同的editText框中输入两个数字,然后使用这些数字来制作一个范围。我想要自定义按钮,当它说"准备好了,"只有当每个盒子里有一个数字时才能工作。但是现在,如果我在edditText框中没有任何内容的情况下点击自定义按钮,程序会给我一个错误。但是,当我在edditText框中输入数字时,该程序可以工作。有什么帮助吗?
编辑:如何检查EditText框中是否有值?
Custom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CBox1.setEnabled(true);
CBox2.setEnabled(true);
Custom.setText("Ready");
Max.setVisibility(View.INVISIBLE);
Min.setVisibility(View.INVISIBLE);
Custom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int b1 = Integer.parseInt(CBox1.getText().toString());
int b2 = Integer.parseInt(CBox2.getText().toString());
if ((b1 > 0) && (b2 > 0)) {
Intent goToCustomMode = new Intent(AdditionDifficulty.this, AdditionHard.class);
startActivity(goToCustomMode);
System.out.println("MY TEXT IS HERE " + Integer.parseInt(CBox1.getText().toString()));
System.out.println("MY TEXT IS HERE " + Integer.parseInt(CBox2.getText().toString()));
}else{
}
}
});
}
});
答案 0 :(得分:0)
我猜你得到的错误包括
Caused by: java.lang.NumberFormatException: Invalid int: ""
您可以在尝试使用之前检查输入: -
int b1 = Integer.parseInt(CBox1.getText().toString());
e.g。
一种解决方案可能是使用以下方法检查输入的内容: -
if (CBox1.getText().toString().length() > 0 &&
CBox2.getText().toString().length() > 0) {
b1 = Integer.parseInt(CBox1.getText().toString());
b2 = Integer.parseInt(CBox2.getText().toString());
// do your stuff here
} else {
// handle no input here e.g.
CBox1.requestFocus();
}
另一种解决方案可能是使用以下内容来捕获错误: -
try {
b1 = Integer.parseInt(CBox1.getText().toString());
b2 = Integer.parseInt(CBox2.getText().toString());
}
catch (Exception e) {
// handle error here e.g.
CBox1.requestFocus();
return;
}
// do your stuff here
第三种解决方案是隐藏按钮,直到有效数据为止。
CBox1 = (EditText) findViewById(R.id.CBox1);
CBox2 = (EditText) findViewById(R.id.CBox2);
doGoButton = (Button) findViewById(R.id.doGoButton);
doGoButton.setVisibility(View.INVISIBLE);
CBox1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (CBox1.getText().toString().length() > 0
&&
CBox2.getText().toString().length() > 0) {
doGoButton.setVisibility(View.VISIBLE);
} else {
doGoButton.setVisibility(View.INVISIBLE);
}
}
});
CBox2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (CBox1.getText().toString().length() > 0
&&
CBox2.getText().toString().length() > 0) {
doGoButton.setVisibility(View.VISIBLE);
} else {
doGoButton.setVisibility(View.INVISIBLE);
}
}
});
请注意,解决方案不会检查无效数据,例如正在输入(它是 假设输入类型已通过布局设置,例如通过使用
android:inputType="number"
)
Custom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CBox1.setEnabled(true);
CBox2.setEnabled(true);
Custom.setText("Ready");
Max.setVisibility(View.INVISIBLE);
Min.setVisibility(View.INVISIBLE);
if (CBox1.getText().toString().length() > 0 &&
CBox2.getText().toString().length() > 0) {
b1 = Integer.parseInt(CBox1.getText().toString());
b2 = Integer.parseInt(CBox2.getText().toString());
if ((b1 > 0) && (b2 > 0)) {
System.out.println("MY TEXT IS HERE " + Integer.parseInt(CBox1.getText().toString()));
System.out.println("MY TEXT IS HERE " + Integer.parseInt(CBox2.getText().toString()));
Intent goToCustomMode = new Intent(AdditionDifficulty.this, AdditionHard.class);
startActivity(goToCustomMode);
} else {
System.out.println("MY TEXT WAS INVALID");
// handle no input here e.g.
CBox1.requestFocus();
}
}
});
请注意!
CBox1.requestFocus
将焦点(即移动光标)放置到 第一个输入。您可能想要检查哪个输入不正确 如果CBox2不正确,请使用CBox2.requestFocus();
。你可能想要 添加system.out.println
表示无效输入。
Custom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CBox1.setEnabled(true);
CBox2.setEnabled(true);
Custom.setText("Ready");
Max.setVisibility(View.INVISIBLE);
Min.setVisibility(View.INVISIBLE);
try {
b1 = Integer.parseInt(CBox1.getText().toString());
b2 = Integer.parseInt(CBox2.getText().toString());
}
catch (Exception e) {
System.out.println("MY TEXT WAS INVALID");
CBox1.requestFocus();
return;
}
// Only invoke AdditionHard activity if input integeres are greater than 0
if ((b1 > 0) && (b2 > 0)) {
Intent goToCustomMode = new Intent(AdditionDifficulty.this, AdditionHard.class);
startActivity(goToCustomMode);
System.out.println("MY TEXT IS HERE " + Integer.parseInt(CBox1.getText().toString()));
System.out.println("MY TEXT IS HERE " + Integer.parseInt(CBox2.getText().toString()));
} else {
System.out.println("MY TEXT HAS A 0 so returning doing nothing!");
}
}
});
请注意!这是在设置onClickListener
之前(或可能在之后)。它为EditTexts
添加了监听器,并且基本上在文本更改时进行检查,例如输入或删除某些内容。然后,当两个文本都存在时,它只会使按钮可见。
Custom.setVisibility(View.INVISIBLE);
CBox1.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (CBox1.getText().toString().length() > 0
&&
CBox2.getText().toString().length() > 0) {
Custom.setVisibility(View.VISIBLE);
} else {
Custom.setVisibility(View.INVISIBLE);
}
}
});
CBox2.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (CBox1.getText().toString().length() > 0
&&
CBox2.getText().toString().length() > 0) {
Custom.setVisibility(View.VISIBLE);
} else {
Custom.setVisibility(View.INVISIBLE);
}
}
});
然后是您现有的代码
Custom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CBox1.setEnabled(true);
CBox2.setEnabled(true);
Custom.setText("Ready");
Max.setVisibility(View.INVISIBLE);
Min.setVisibility(View.INVISIBLE);
...........etc.....
您是否希望在调用的活动中获得值 b1 和 b2 ,如果是这样,您应该将值放入Intent中,例如在startActivity(goToCustomMode);
之前?
Intent goToCustomMode = new Intent(AdditionDifficulty.this, AdditionHard.class);
intent.putExtra("B1VALUE",b1);
intent.putExtra("B2VALUE",b2);
startActivity(goToCustomMode);
然后在调用的活动中使用以下内容来检索值: -
int passedb1 = getIntent().getIntExtra("B1VALUE",0);
int passedb2 = getIntent().getIntExtra("B2VALUE",0);
请注意!关键,例如 B1VALUE 必须匹配推杆时使用的键
intentExtra
标识值的是什么。另外 如果不匹配,getIntExtra
需要返回默认值 发现,所以我在这里使用了0.你必须检查一下 检索到的值不为0(即未找到相应的intentExtra
)。