【发布时间】:2014-08-23 12:32:48
【问题描述】:
我有一个自定义对话框,其中包含一个 EditText 一个 按钮(“默认”) 和 确定/取消按钮 我完美地对对话框进行了编程,因此用户在 EditText 中输入一个值,然后选择确定接受或取消以取消对话框
我的问题是我想对 默认按钮 进行编程以调用确定按钮... 我也想知道如何关闭对话框(也可以通过默认按钮)
这是我的代码
LayoutInflater li = LayoutInflater.from(getActivity());
View promptsView = li.inflate(R.layout.prompt, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());
alertDialogBuilder.setView(promptsView);
EditText userInput = (EditText) promptsView.findViewById(R.id.edtitext);
Button defaultButton = (Button) promptsView.findViewById(R.id.button);
defaultButton.setOnClickListener(new setOnClickListener() {
@Override
public boolean setOnClickListener()
{
//default
//how to call the ok button actions
//how to just close the dialog
return false;
}
};
alertDialogBuilder.setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
//ok actions
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
【问题讨论】:
-
只需在单击按钮时调用 dialog.dismiss() 函数,即可轻松取消对话框
-
对话框变量是什么意思??
-
就像你用 alertDialog.show 显示对话框一样简单地调用 alertDialog.dismiss() 来取消对话框
-
好的,那我怎么调用 ok 按钮操作