将两个自定义的AlertDialog封装成工具类,直接拿来用!
先上图:
样式一 样式二其中样式一用来弹出提示消息,样式二用来做简单的选择很合适。
上代码
AlertDialogUtils.java
public class AlertDialogUtils {
public static AlertDialogUtils getInstance() {
return new AlertDialogUtils();
}
/**
* 弹出自定义样式的AlertDialog
*
* @param context 上下文
* @param title AlertDialog的标题
* @param tv 点击弹出框选择条目后,要改变文字的TextView
* @param args 作为弹出框中item显示的字符串数组
*/
public void showAlertDialog(Context context, String title, final TextView tv, final List<String> args) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final AlertDialog dialog = builder.create();
dialog.show();
View view = LayoutInflater.from(context).inflate(R.layout.alert_dialog_salary, null);
TextView tvTitle = (TextView) view.findViewById(R.id.tv_title_alert_dialog_salary);
ListView list = (ListView) view.findViewById(R.id.lv_alert_dialog_salary);
tvTitle.setText(title);
ListAdapter adpter = new ArrayAdapter<String>(context, R.layout.item_listview_salary, R.id.tv_item_listview_salary, args);
list.setAdapter(adpter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String str = args.get(position);
tv.setText(str);
if (onDialogItemSelectListener != null) {
onDialogItemSelectListener.onItemSelect(str);
}
dialog.dismiss();
}
});
dialog.getWindow().setContentView(view);
}
private OnDialogItemSelectListener onDialogItemSelectListener;
public void setOnDialogItemSelectListener(AlertDialogUtils.OnDialogItemSelectListener onDialogItemSelectListener) {
this.onDialogItemSelectListener = onDialogItemSelectListener;
}
/**
* item选中回调接口
*/
public interface OnDialogItemSelectListener {
/**
* item选中回调方法
*
* @param str 选中的item中的String
*/
void onItemSelect(String str);
}
/**
* 带有确认取消按钮的自定义dialog
*
* @param context 上下文
* @param message 显示的信息
*/
public static void showConfirmDialog(Context context, String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
final AlertDialog alertDialog = builder.create();
alertDialog.show();
View view = View.inflate(context, R.layout.view_alert_dialog_confirm, null);
TextView tvMsg = (TextView) view.findViewById(R.id.tv_message_dialog);
TextView tvCancel = (TextView) view.findViewById(R.id.tv_cancel_dialog);
TextView tvConfirm = (TextView) view.findViewById(R.id.tv_confirm_dialog);
tvMsg.setText(message);
tvCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onButtonClickListener.onNegativeButtonClick(alertDialog);
}
});
tvConfirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onButtonClickListener.onPositiveButtonClick(alertDialog);
}
});
alertDialog.getWindow().setContentView(view);
}
private static OnButtonClickListener onButtonClickListener;
public void setOnButtonClickListener(OnButtonClickListener onButtonClickListener) {
this.onButtonClickListener = onButtonClickListener;
}
/**
* 按钮点击回调接口
*/
public interface OnButtonClickListener {
/**
* 确定按钮点击回调方法
*
* @param dialog 当前 AlertDialog,传入它是为了在调用的地方对 dialog 做操作,比如 dismiss()
* 也可以在该工具类中直接 dismiss() 掉,就不用将 AlertDialog 对象传出去了
*/
void onPositiveButtonClick(AlertDialog dialog);
/**
* 取消按钮点击回调方法
*
* @param dialog 当前AlertDialog
*/
void onNegativeButtonClick(AlertDialog dialog);
}
}
样式一:带确定取消按钮的布局文件 view_alert_dialog_confirm.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/confirm_dialog_bg"
android:layout_centerInParent="true"
android:minWidth="280dp"
>
<TextView
android:id="@+id/tv_message_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="100dp"
android:padding="30dp"
tools:text="确定删除此条信息?"
android:textColor="@color/richeng_bg"
android:textSize="16sp"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/divider_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
>
<TextView
android:id="@+id/tv_cancel_dialog"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:text="取消"
android:textColor="@color/richeng_bg"
android:textSize="16sp"
android:background="@drawable/selector_dialog_btn_cancel"
/>
<TextView
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="@color/divider_color" />
<TextView
android:id="@+id/tv_confirm_dialog"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:padding="5dp"
android:text="确定"
android:textColor="@color/chayue_5"
android:textSize="16sp"
android:background="@drawable/selector_dialog_btn_confirm"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
样式二:带标题的布局文件 alert_dialog_salary.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/tv_title_alert_dialog_salary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:paddingBottom="15dp"
android:paddingLeft="20dp"
android:paddingTop="15dp"
android:text="我是标题"
android:textColor="@color/black"
android:textSize="16sp" />
<!--分割线-->
<TextView
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/divider_color" />
<ListView
android:id="@+id/lv_alert_dialog_salary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"></ListView>
</LinearLayout>
里面的背景选择器比较简单就不贴了。
使用方法
样式一:
AlertDialogUtils utils = AlertDialogUtils.getInstance();
utils.showConfirmDialog(this, "确定删除本条通知公告吗?");
//按钮点击监听
utils.setOnButtonClickListener(new AlertDialogUtils.OnButtonClickListener() {
@Override
public void onPositiveButtonClick(AlertDialog dialog) {
dialog.dismiss();
//做自己的操作
}
@Override
public void onNegativeButtonClick(AlertDialog dialog) {
dialog.dismiss();
}
});
样式二:
//获取 AlertDialogUtils 对象
AlertDialogUtils alertDialogUtils = AlertDialogUtils.getInstance();
//传入参数:tvDepartment是选择完之后要改变文字内容的TextView
alertDialogUtils.showAlertDialog(ConferenceSendActivity.this, "请选择部门", tvDepartment, names);
网友评论