android dialog自定义按钮如何设监听事件
展开全部
利用回调方悔枝兆法,在调用处,进行Dialog中各按钮的事件处搭旦理。
view plaincopy
package com.demo;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class LeaveMeetingDialog extends Dialog implements OnClickListener{
private Button quitBtn,stopBtn,cancelBtn;
private LeaveMeetingDialogListener listener;
public interface LeaveMeetingDialogListener{
public void onClick(View view);
}
public LeaveMeetingDialog(Context context,int theme,LeaveMeetingDialogListener listener) {
super(context,theme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.leave_meeting_dialog);
initViews();
}
private void initViews(){
quitBtn = (Button)findViewById(R.id.quit_btn);
stopBtn = (Button)findViewById(R.id.stop_btn);
cancelBtn = (Button)findViewById(R.id.cancel_btn);
quitBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this);
cancelBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
listener.onClick(v);
}
}
---调用碧租处:
[java]
LeaveMeetingDialog dialog = new LeaveMeetingDialog(this,R.style.Theme_CustomDialog,
new LeaveMeetingDialogListener() {
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.quit_btn:break;
case R.id.stop_btn:break;
case R.id.cancel_btn:break;
}
}
});
dialog.show();
view plaincopy
package com.demo;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class LeaveMeetingDialog extends Dialog implements OnClickListener{
private Button quitBtn,stopBtn,cancelBtn;
private LeaveMeetingDialogListener listener;
public interface LeaveMeetingDialogListener{
public void onClick(View view);
}
public LeaveMeetingDialog(Context context,int theme,LeaveMeetingDialogListener listener) {
super(context,theme);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.leave_meeting_dialog);
initViews();
}
private void initViews(){
quitBtn = (Button)findViewById(R.id.quit_btn);
stopBtn = (Button)findViewById(R.id.stop_btn);
cancelBtn = (Button)findViewById(R.id.cancel_btn);
quitBtn.setOnClickListener(this);
stopBtn.setOnClickListener(this);
cancelBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
listener.onClick(v);
}
}
---调用碧租处:
[java]
LeaveMeetingDialog dialog = new LeaveMeetingDialog(this,R.style.Theme_CustomDialog,
new LeaveMeetingDialogListener() {
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.quit_btn:break;
case R.id.stop_btn:break;
case R.id.cancel_btn:break;
}
}
});
dialog.show();
展开全部
跟普通的按钮族汪虚操作一陵乎样,但是注意括号内加上“View.” ,兆燃否则会报错
button.setOnClickListener(new View.OnClickListener){ …… }
button.setOnClickListener(new View.OnClickListener){ …… }
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询