android 检查 EditText 是否为空,然后避免取消对话框
android check if EditText is empty and then avoid dialog from canceling
嘿,在我的 android 应用程序中,我正在尝试使用 EditText 创建一个对话框。我的代码问题是,如果我的 EditTex 为空,应用程序将无法运行。因此,我试图检查 EditText 是否为空,并在 EditText 为空时防止对话框取消。
提前致谢
代码如下:
private void AdPoint() {
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
LinearLayout linearlayout= new LinearLayout(this);
linearlayout.setOrientation(1);
final EditText point_field = new EditText(this);
final String point_field_string = point_field.getText().toString();
point_field.setImeOptions(InputType.TYPE_CLASS_NUMBER);
point_field.setRawInputType(Configuration.KEYBOARD_12KEY);
linearlayout.addView(point_field);
alert.setView(linearlayout);
alert.setCancelable(false);
alert.setTitle(playername1string);
alert.setMessage(R.string.how_many_point);
point_field.setHint("10");
playername1.setText(playername1string);
alert.setPositiveButton(R.string.plus, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (point_field_string.matches("")) {
//My code, if the EditText is not emty
return;
}else {
point_field.setText("0");
}
}
});
alert.show();
}
改变这个:
if (point_field_string.matches("")) {
//My code, if the EditText is not emty
return;
}
else {
point_field.setText("0");
}
至:
if(point_field_string.getText()!=null){
if (point_field_string.getText().toString().length()>0) {
//My code, if the EditText is not emty
return;
}
else {
point_field.setText("0");
}
}
使用这个...
if(TextUtils.isEmpty(point_field_string)){
// code to handle if field is empty
}else{
// code to handle if field is non-empty
}
嘿,在我的 android 应用程序中,我正在尝试使用 EditText 创建一个对话框。我的代码问题是,如果我的 EditTex 为空,应用程序将无法运行。因此,我试图检查 EditText 是否为空,并在 EditText 为空时防止对话框取消。 提前致谢
代码如下:
private void AdPoint() {
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
LinearLayout linearlayout= new LinearLayout(this);
linearlayout.setOrientation(1);
final EditText point_field = new EditText(this);
final String point_field_string = point_field.getText().toString();
point_field.setImeOptions(InputType.TYPE_CLASS_NUMBER);
point_field.setRawInputType(Configuration.KEYBOARD_12KEY);
linearlayout.addView(point_field);
alert.setView(linearlayout);
alert.setCancelable(false);
alert.setTitle(playername1string);
alert.setMessage(R.string.how_many_point);
point_field.setHint("10");
playername1.setText(playername1string);
alert.setPositiveButton(R.string.plus, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (point_field_string.matches("")) {
//My code, if the EditText is not emty
return;
}else {
point_field.setText("0");
}
}
});
alert.show();
}
改变这个:
if (point_field_string.matches("")) {
//My code, if the EditText is not emty
return;
}
else {
point_field.setText("0");
}
至:
if(point_field_string.getText()!=null){
if (point_field_string.getText().toString().length()>0) {
//My code, if the EditText is not emty
return;
}
else {
point_field.setText("0");
}
}
使用这个...
if(TextUtils.isEmpty(point_field_string)){
// code to handle if field is empty
}else{
// code to handle if field is non-empty
}