调用对话框的ok按钮

时间:2014-08-23 12:32:49

标签: android dialog action

我有一个自定义对话框,其中包含 EditText 一个按钮("默认")以及确定/取消按钮 我完美地编写了对话框,以便用户为EditText输入一个值,然后选择ok接受或取消取消对话框

我的问题是我想编程默认按钮来调用ok按钮... 我也想知道如何关闭对话框(也来自默认按钮)

这是我的代码

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();

1 个答案:

答案 0 :(得分:0)

您已经为负按钮设置了侦听器,对于正按钮也是如此:

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

    }
});

如果您想要关闭对话框,请致电:

dialog.dismiss();
相关问题