我已经使用了自定义对话框并在其中添加了EditText
,当用户点击确定按钮而不输入他/她时,我会通过显示错误消息来验证EditText
这里是名字,但是当我运行它并点击确定按钮时,无论是否输入名称都是例外
发生了为什么会这样?请帮帮我..
这是我的代码
final Dialog nameDialog =new Dialog(ProfileView.this);
nameDialog.setContentView(R.layout.namedialog);
nameDialog.setTitle("please enter your name");
name =(EditText)nameDialog.findViewById(R.id.nameETDialog);
Button okButton = (Button) nameDialog.findViewById(R.id.ok);
okButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//validate the name
if( name.getText().toString()== null)
name.setError( " your name is requiered" );
else
{
EditText nametext=(EditText)findViewById(R.id.name_text);
nametext.setText(name.getText().toString());
Toast.makeText(getBaseContext(),"your name has been entered ", Toast.LENGTH_SHORT).show();
}
}
});
Button cancelButton = (Button) nameDialog.findViewById(R.id.cancel);
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nameDialog.dismiss();
}
});
nameDialog.show();
}
});
}
}
答案 0 :(得分:0)
查看代码,没有Logcat,我认为你必须改变:
name =(EditText)nameDialog.findViewById(R.id.nameETDialog);
Button okButton = (Button) nameDialog.findViewById(R.id.ok);
使用
name =(EditText) findViewById(R.id.nameETDialog);
Button okButton = (Button) findViewById(R.id.ok);
我认为按钮okButton
位于名为namedialog