【问题标题】:call the ok button of a dialog调用对话框的确定​​按钮
【发布时间】: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 按钮操作

标签: android dialog action


【解决方案1】:

你已经设置了负按钮的监听器,正按钮也是如此:

.setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {

    }
});

如果你想关闭对话框,只需调用:

dialog.dismiss();

【讨论】:

  • 再次......我想编程默认按钮来调用 ok 按钮......我已经完成了 ok 正如你在上面的代码中看到的那样
猜你喜欢
  • 2013-06-18
  • 1970-01-01
  • 2010-10-27
  • 2012-01-10
  • 1970-01-01
  • 2012-06-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多