激活Android对话框中的按钮

时间:2015-12-16 06:52:45

标签: android button onclicklistener dialog

当我在onListItemClick中添加按钮功能时出现错误,这是我的代码。

{{1}}

我想激活对话框中的按钮。 我该怎么办?

谢谢!

2 个答案:

答案 0 :(得分:2)

使用dialog代替getActivity();

Button btnYes = (Button) dialog.findViewById(R.id.yes);
Button btnNo = (Button) dialog.findViewById(R.id.no);

答案 1 :(得分:1)

用这些代码行替换代码

    public void onListItemClick(ListView l, View view, int position, long id){
    final Dialog dialog = new Dialog(this.getActivity());
    dialog.setTitle("Dicoba dialog");
    dialog.setContentView(R.layout.my_dialog);
    dialog.show();

    ///// IN PLACE OF FINDING IDS OF THE BUTTON FROM GETVIEW...YOU SHOULD UES THE DIALOG REFERENCE AS BELOW:-


    Button btnYes = (Button) dialog.findViewById(R.id.yes);
    Button btnNo = (Button) dialog.findViewById(R.id.no);

    btnYes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(getActivity(), "Yes!!", Toast.LENGTH_LONG).show();
            dialog.cancel();
        }
    });

    btnNo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.cancel();
        }
    });
}