adding OnClick Listeners to buttons in a custom dialog
I have a custom dialog class as follows where xmlView = R.layout.yourdialoglayout which has 2 buttons. How could I add listeners to these buttons?
heres my class:
public class CustomDialog extends Dialog {
public CustomDialog(Context context,int theme,int xmlView) {
super(context,theme);
requestWindowFeature(Window.FEATURE_NO_TITLE); //Hide the title
this.setContent开发者_Go百科View(xmlView);
}
public void killDialog() {
dismiss();
}
}
You can simply attach an OnClickListener just as you would for an Activity, by using View.SetOnClickListener:
public CustomDialog(Context context, int theme, int xmlView)
{
super(context,theme);
requestWindowFeature(Window.FEATURE_NO_TITLE); // hide the title
this.setContentView(xmlView);
// your special button
Button yourButton = findViewById(R.id.yourbutton);
yourButton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// your action
}
});
}
You can attach an action to your other button the same way.
You can use findViewById
to find the buttons, and set OnClickListener
on them as usual
精彩评论