我通过检查空输入并调用setError
来检查警报对话框中一组编辑文本的无效输入。但是在我当前的实现中,即使输入无效,对话框仍然会关闭。
每个编辑文本都添加了一个布尔检查,以防止在任何编辑文本将布尔值设置为false时解除对话框,如下所示:
else if(TextUtils.isEmpty(strColour)) {
colourText.setError("Please enter a value");
entriesValid = false;
` 但是,尽管输入无效,对话仍然被驳回。
我的问题,这里的错误是什么,允许对话框关闭无效输入?
我在这一行if(entriesValid)
设置了一个断点,以检查条件是否被触发但是它没有在此处中断,这意味着将跳过该检查。
这是完整的自定义对话框类:
public class MyMessageDialog {
public interface MyMessageDialogListener {
public void onClosed(String ship, String scientist, String email, String volume, String color);
}
@SuppressLint("NewApi")
public static AlertDialog displayMessage(Context context, String title, String message, final MyMessageDialogListener listener){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = LayoutInflater.from(context);
builder.setTitle(title);
builder.setMessage(message);
final View layoutView = inflater.inflate(R.layout.custom_view, null);
builder.setView(layoutView);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean entriesValid = true;
// get the edit text values here and pass them back via the listener
if(listener != null)
{
EditText shipText = (EditText)layoutView.findViewById(R.id.shipNameEditText);
EditText scientistNameText = (EditText)layoutView.findViewById(R.id.scientistEditText);
EditText scientistEmailText = (EditText)layoutView.findViewById(R.id.emailEditText);
EditText volumeText = (EditText)layoutView.findViewById(R.id.volumeEditText);
EditText colourText = (EditText)layoutView.findViewById(R.id.colourEditText);
listener.onClosed(shipText.getText().toString(),
scientistNameText.getText().toString(),
scientistEmailText.getText().toString(),
volumeText.getText().toString(),
colourText.getText().toString());
String strShipName = shipText.getText().toString();
String strScientistName = scientistNameText.getText().toString();
String strScientistEmail = scientistEmailText.getText().toString();
String strVolume = volumeText.getText().toString();
String strColour = colourText.getText().toString();
if(TextUtils.isEmpty(strShipName)) {
shipText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strShipName)) {
shipText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strScientistName)) {
scientistNameText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strScientistEmail)) {
scientistEmailText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strVolume)) {
volumeText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strColour)) {
colourText.setError("Please enter a value");
entriesValid = false;
}
}
if(entriesValid)
dialog.dismiss();
}
});
builder.show();
return builder.create();
}
}
答案 0 :(得分:1)
不是将侦听器检查为null,而是添加try catch块。我没有尝试过这段代码。但我的想法是用try catch删除监听器块并相应地设置布尔标志。这样就变得简单了。
@SuppressLint("NewApi")
public static AlertDialog displayMessage(Context context, String title, String message, final MyMessageDialogListener listener){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = LayoutInflater.from(context);
builder.setTitle(title);
builder.setMessage(message);
final View layoutView = inflater.inflate(R.layout.custom_view, null);
builder.setView(layoutView);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
boolean entriesValid = true;
// get the edit text values here and pass them back via the listener
try
{
EditText shipText = (EditText)layoutView.findViewById(R.id.shipNameEditText);
EditText scientistNameText = (EditText)layoutView.findViewById(R.id.scientistEditText);
EditText scientistEmailText = (EditText)layoutView.findViewById(R.id.emailEditText);
EditText volumeText = (EditText)layoutView.findViewById(R.id.volumeEditText);
EditText colourText = (EditText)layoutView.findViewById(R.id.colourEditText);
String strShipName = shipText.getText().toString();
String strScientistName = scientistNameText.getText().toString();
String strScientistEmail = scientistEmailText.getText().toString();
String strVolume = volumeText.getText().toString();
String strColour = colourText.getText().toString();
if(TextUtils.isEmpty(strShipName)) {
shipText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strShipName)) {
shipText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strScientistName)) {
scientistNameText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strScientistEmail)) {
scientistEmailText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strVolume)) {
volumeText.setError("Please enter a value");
entriesValid = false;
}
else if(TextUtils.isEmpty(strColour)) {
colourText.setError("Please enter a value");
entriesValid = false;
}
}
catch(Exception e)
{
entriesValid = false;
}
if(entriesValid)
dialog.dismiss();
}
});
builder.show();
return builder.create();
}
更新 - 新解决方案 - 尝试并为我工作
public class Help_DialogScreen extends Dialog implements OnClickListener{
Context context;
public Help_DialogScreen(Context context) {
super(context);
// TODO Auto-generated constructor stub
this.context=context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.help_homescreen);
EditText tvGoToAddExpense = (EditText)findViewById(R.id.txtGoToAddExpense);
Button btnTestCLick = (Button)findViewById(R.id.btnTestClick);
btnTestCLick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "Click fired", Toast.LENGTH_SHORT).show();
// I have used Toast to show that on click of button, dialog is not getting dismissed. You can add your code and do your logic here.
}
});
}
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
}
在您应该显示对话框的代码中,添加此代码
Help_DialogScreen cdd=new Help_DialogScreen(CURRENTACTIVITY.this);
cdd.show();