以下是我为2个对话框创建的代码:即提交2个按钮的日期选择器和自定义对话框,即btnselDate和btnAlertDialog。
“日期选择器”对话框工作正常,但自定义对话框存在问题。 我的自定义对话框显示用户的登录表单。
请审核并提出相应的建议。
以下是代码:
public void onClick(View view)
{
if(view.getId() == R.id.btnselDate)
{
// Date Picket DialogBox
showDialog(1);
}
else if(view.getId()==R.id.btnAlertDialog)
{
// Alert Dialog Box
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_activity);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.tvPwd);
text.setText("Enter the Password");
final EditText pwd=(EditText) dialog.findViewById(R.id.etPwd);
Button btnlogin=(Button) dialog.findViewById(R.id.btnOK);
btnlogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//Login Button
if(pwd.getText().toString().equals("abc"))
{
Intent intent=new Intent(MainActivity.this,WelcomeUser.class);
startActivity(intent);
}
else
{
Toast.makeText(MainActivity.this, "Wrong Password, Try Again", Toast.LENGTH_SHORT).show();
}
}
});
}
else
{
Toast.makeText(MainActivity.this, "No Dialog Selected yet", Toast.LENGTH_SHORT).show();
}
}
答案 0 :(得分:0)
而不是
dialog.setContentView(R.layout.custom_activity);
对于对话框的自定义布局,您必须对布局进行膨胀,请检查此处的代码
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
return new AlertDialog.Builder(AlertDialogSamples.this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_text_entry)
.setView(textEntryView)
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked OK so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked cancel so do some stuff */
}
})
.create();