Android—关于自定义对话框的工具类

开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类。

弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函数的参数中,并且是静态,可以用类直接调用此函数。

public class MyAutoDialogUtil {

	private static AlertDialog dialog;

	/**
	 * 
	 * @param context
	 *            上下文
	 * @param text
	 *            自定义显示的文字
	 * @param id
	 *            自定义图片资源
	 */
	public static void showScanNumberDialog(final Context context, String text,
			int id) {
		// SysApplication.getInstance().exit();
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		// 创建对话框
		dialog = builder.create();
		// 没有下面这句代码会导致自定义对话框还存在原有的背景

		// 弹出对话框
		dialog.show();
		// 以下两行代码是对话框的EditText点击后不能显示输入法的
		dialog.getWindow().clearFlags(
				WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
						| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
		dialog.getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
		// *** 主要就是在这里实现这种效果的.
		// 设置窗口的内容页面,shrew_exit_dialog.xml文件中定义view内容
		Window window = dialog.getWindow();
		window.setContentView(R.layout.auto_dialog);
		TextView tv_scan_number = (TextView) window
				.findViewById(R.id.tv_dialoghint);
		tv_scan_number.setText(text);
		// 实例化确定按钮
		Button btn_hint_yes = (Button) window.findViewById(R.id.btn_hint_yes);
		// 实例化取消按钮
		Button btn_hint_no = (Button) window.findViewById(R.id.btn_hint_no);
		// 实例化图片
		ImageView iv_dialoghint = (ImageView) window
				.findViewById(R.id.iv_dialoghint);
		// 自定义图片的资源
		iv_dialoghint.setImageResource(id);
		btn_hint_yes.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "确定", 0).show();
			}
		});
		btn_hint_no.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Toast.makeText(context, "取消", 0).show();
			}
		});
	}

	public static void dismissScanNumberDialog() {
		dialog.dismiss();
	}

}

 对话框的xml文件布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/app_customs_autodialog_background"
    android:gravity="center"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:padding="16dp" >

        <ImageView
            android:id="@+id/iv_dialoghint"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="16dp"
            android:src="@drawable/app_customs_choose_background" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="提示"
                android:textColor="#000"
                android:textSize="22sp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:text="点击确定进入"
                android:textColor="#f00"
                android:textSize="20sp" />

            <TextView
                android:id="@+id/tv_dialoghint"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:text="下一步 >>"
                android:textColor="#f00"
                android:textSize="20sp" />
        </LinearLayout>
    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:background="#000" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_hint_yes"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/app_custom_changeactivity_btn_yess"
            android:padding="8dp"
            android:text="确定"
            android:textColor="#1e9ee1"
            android:textSize="20sp" />

        <View
            android:layout_width="0.1dp"
            android:layout_height="match_parent"
            android:background="#000" />

        <Button
            android:id="@+id/btn_hint_no"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/app_custom_changeactivity_btn_yess"
            android:padding="8dp"
            android:text="取消"
            android:textColor="#1e9ee1"
            android:textSize="20sp" />
    </LinearLayout>

</LinearLayout>

 Activity中弹出对话框函数的实现:

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button button1=(Button) findViewById(R.id.button1);
		button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				MyAutoDialogUtil.showScanNumberDialog(MainActivity.this, "自定义文字", R.drawable.app_customs_choose_background);
			}
		});
	}


}

 MainActivity中调用了MyAutoDialogUtil类中的showScanNumberDialog()方法,并将上下文,自定义的内容和图片的资源传递到参数中从而得到需要的效果。

上图:

源码:https://yunpan.cn/cSUzmuBiRGhtH  访问密码 0377

欢迎提意见,希望可以给大家带来帮助~

posted @ 2016-05-31 23:15  奋斗者—cyf  阅读(2939)  评论(0编辑  收藏  举报