简体   繁体   English

Android自定义对话框中的按钮

[英]Button in Custom Dialog Android

This is my code 这是我的代码

    protected void markerTouched(Marker marker) {


                final Dialog dialog = new Dialog(context);


                dialog.setContentView(R.layout.custom_dialog);


                dialog.setCanceledOnTouchOutside(true);

                dialog.show();      

                 Button declineButton = (Button) dialog.findViewById(R.id.b_close);
                    // if decline button is clicked, close the custom dialog
                    declineButton.setOnClickListener(new OnClickListener() {
                        public void onClick(View v) {
                            // Close dialog
                            dialog.dismiss();
                        }
                    });
     }

i have found an error in this line: 我在这一行发现了一个错误:

 declineButton.setOnClickListener(new OnClickListener() 

the error on setOnClickListner : setOnClickListner上的错误:

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (new DialogInterface.OnClickListener(){}) 视图类型中的方法setOnClickListener(View.OnClickListener)不适用于自变量(新DialogInterface.OnClickListener(){})

and other in OnClickListener (): 和其他在OnClickListener ()中:

The type new DialogInterface.OnClickListener(){} must implement the inherited abstract method DialogInterface.OnClickListener.onClick(DialogInterface, int) 新类型DialogInterface.OnClickListener(){}必须实现继承的抽象方法DialogInterface.OnClickListener.onClick(DialogInterface,int)

someone can explain what i've to do? 有人可以解释我该怎么做?

Try this: 尝试这个:

dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");


Button dialog_btn = (Button) dialog.findViewById(R.id.dialog_button);
dialog_btn.setOnClickListener(new View.OnClickListener() 
{
    // Perform button logic
}

this is important: 这个很重要:

dialog_btn.setOnClickListener(new View.OnClickListener() 

View.OnClcikListener View.OnClcikListener

Hope this helps. 希望这可以帮助。

you have the wrong import. 您输入错误。 Since you are setting onClickListener for a Button you need View.OnClickListener but the import is DialogInterface.OnClickListener 既然你设置onClickListener一个按钮,你需要View.OnClickListener但进口是DialogInterface.OnClickListener

please try 请试试

declineButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
                        // TODO Auto-generated method stub

                    }




});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM