android 学习随笔二十九(自定义监听 )

package com.itheima.momo.dialog;

import com.itheima.momo.R;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

public abstract class BaseDialog extends AlertDialog implements android.view.View.OnClickListener{

protected BaseDialog(Context context) {
//通过构造指定主题,主题中就已经设置了弧形边角的背景
super(context, R.style.BaseDialog);
// TODO Auto-generated constructor stub
}

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
initView();
initListener();
initData();
}

public abstract void initView();
public abstract void initListener();
public abstract void initData();
public abstract void processClick(View v);


/*
* 这个是继承得来的方法,用这个方法后,一个activity里面所有组件的按钮都只要去
* 实现抽象方法就行,组件上就会自动的挂上侦听,而不用每个组件都写一个点击事件
* 了,这样可以提高代码的复用性,还有代码的间接性。
*/
@Override
public void onClick(View v) {
processClick(v);

}
}
package com.itheima.momo.dialog;

import com.itheima.momo.R;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ConfirmDialog extends BaseDialog {

private String title;
private String message;
private TextView tv_dialog_title;
private TextView tv_dialog_message;
private Button bt_dialog_cancel;
private Button bt_dialog_confirm;
private OnConfirmListener onConfirmListener;

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getMessage() {
return message;
}

public void setMessage(String message) {
this.message = message;
}

protected ConfirmDialog(Context context) {
super(context);
// TODO Auto-generated constructor stub
}

//在类中写一个静态方法,直接获取本类的对象。
public static void showDialog(Context context, String title, String message, OnConfirmListener confirmListener){
ConfirmDialog dialog = new ConfirmDialog(context);
dialog.setTitle(title);
dialog.setMessage(message);
dialog.setConfirmListener(confirmListener);
dialog.show();
}

@Override
public void initView() {
//设置对话框显示的布局文件
setContentView(R.layout.dialog_confirm);

tv_dialog_title = (TextView) findViewById(R.id.tv_dialog_title);
tv_dialog_message = (TextView) findViewById(R.id.tv_dialog_message);

bt_dialog_cancel = (Button) findViewById(R.id.bt_dialog_cancel);
bt_dialog_confirm = (Button) findViewById(R.id.bt_dialog_confirm);

}

@Override
public void initListener() {
bt_dialog_cancel.setOnClickListener(this);
bt_dialog_confirm.setOnClickListener(this);

}

@Override
public void initData() {
tv_dialog_title.setText(title);
tv_dialog_message.setText(message);

}

@Override
public void processClick(View v) {
switch (v.getId()) {
//如果取消按钮按下时,侦听存在,那么调用侦听的onCancel
case R.id.bt_dialog_cancel:
if(onConfirmListener != null){
onConfirmListener.onCancel();
}
break;
case R.id.bt_dialog_confirm:
if(onConfirmListener != null){
onConfirmListener.onConfirm();
}
break;
}
//对话框消失
dismiss();
}

public void setConfirmListener(OnConfirmListener confirmListener) {
this.onConfirmListener = confirmListener;
}

//自己定义一个接口,为了保证其能够被多次复用。
public interface OnConfirmListener{
void onCancel();
void onConfirm();
}
}

---------------------------------------------------------------------------------------------

private void showDeleteDialog() {
ConfirmDialog.showDialog(getActivity(), "提示", "真的要删除会话吗?", new OnConfirmListener() {

@Override
public void onConfirm() {
deleteSms();
}

@Override
public void onCancel() {
}
});

}

posted @ 2016-10-21 18:14  ecollab  阅读(231)  评论(0编辑  收藏  举报