在做项目时dialog很常用,很多时候原生的dialog不符合我们的要求,这就需要自己定义自己想要的效果。
要更改dialog的效果,就需要继承Dialog这个类:
/**
* 自定义dialog
*/
class CustomDialog extends Dialog implements
View.OnClickListener {
/**
* 布局文件
**/
int layoutRes;
/**
* 上下文对象
**/
Context context;
/**
* 取消按钮
**/
private Button bt_cancal;
/**
* 删除按钮
**/
private Button bt_delect;
/**
* 位置
*/
private int postion;
public CustomDialog(Context context) {
super(context);
this.context = context;
}
/**
* 自定义布局的构造方法
*
* @param context
* @param resLayout
*/
public CustomDialog(Context context, int resLayout) {
super(context);
this.context = context;
this.layoutRes = resLayout;
}
/**
* 自定义主题及布局的构造方法
*
* @param context
* @param theme
* @param resLayout
* @param postion
*/
public CustomDialog(Context context, int theme, int resLayout,
int postion) {
super(context, theme);
this.context = context;
this.layoutRes = resLayout;
this.postion = postion;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 指定布局
this.setContentView(layoutRes);
// 根据id在布局中找到控件对象
bt_cancal = (Button) findViewById(R.id.bt_cancal);
bt_delect = (Button) findViewById(R.id.bt_delect);
// 为按钮绑定点击事件监听器
bt_cancal.setOnClickListener(this);
bt_delect.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
// 删除按钮
case R.id.bt_delect:
// 删除数据的方法
deleteItem(postion);
dialog.dismiss();
break;
// 取消按钮
case