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

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

时间: 2024-08-01 22:38:53

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

【转载】Android应用框架及常用工具类总结

转载自:Android应用框架 http://www.tuicool.com/articles/feqmQj 常用工具类总结    http://blog.csdn.net/krislight/article/details/11354119 一. UML类图复习: UML类图中有些标记很容易混淆,这里先复习下,请大家看下面这幅图: 注:这幅图来自<大话设计模式>这本书中的插图. 二.应用框架: A.基本概念 抽象(抽出共同之现象)——在同领域的程序中,常含有许多类别,这些类别有其共同点,我们

Android快速开发系列 常用工具类

1.日志工具类L.java package com.way.common.util; import android.util.Log; /** * Log统一管理类 * * @author way * */ public class L { public static boolean isDebug = true;// 是否需要打印bug,可以在application的onCreate函数里面初始化 private static final String TAG = "way"; //

Android之SharedPreferences两个工具类

相信Android的这个最简单的存储方式大家都很熟悉了,但是有一个小小技巧,也许你没有用过,今天就跟大家分享一下,我们可以把SharedPreferences封装在一个工具类中,当我们需要写数据和读数据的时候,就可以直接通过工具类的set和get方法来完成,类似JavaBean,这样使用起来就比较方便,快捷(建议项目中使用次数比较多使用).好了,直接看看这段简单的代码吧: public class SharePreferenceUtil { private SharedPreferences s

Android加载网络图片的工具类

ImageView加载网络的图片 HttpUtil.java package com.eiice.httpuimagetils; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.graphics.Bitmap; import android.util.Log; /** *

Android 分享一个SharedPreferences的工具类,方便保存数据

我们平常保存一些数据,都会用到SharedPreferences,他是保存在手机里面的,具体路径是data/data/你的包名/shared_prefs/保存的文件名.xml, SharedPreferences的使用也很简单,我自己就写了一个SharedPreferences的工具类,然后就保存在这里,等自己以后需要保存数据直接从这里copy代码,哈哈 工具类如下 [java] view plaincopy package com.example.shortcut; import androi

Android中创建倒影效果的工具类

一.有时候我们需要创建倒影的效果,我们接触最多的都是图片能够创建倒影,而布局依然可以创建倒影. 二.工具类代码 import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.LinearGradient; import android.graphics.Matrix; import android.grap

android获取屏幕宽高工具类

import java.lang.reflect.Field; import android.app.Activity; import android.content.Context; import android.graphics.Point; import android.util.DisplayMetrics; import android.view.Display; import android.view.Window; import android.view.WindowManager

Android Studio快速创建常用工具类的插件Utils

现如今Android开发,开发工具Android Studio已成为主流,而为Android Studio打造的插件也越来越多,今天为大家介绍一个快速创建常用工具类的插件Utils.其实Android中有关工具类的库有很多,但我们开发中一般只会用到某个库的一个或几个类,所以这时候Utils就有了很大的优势了,它直接创建自己所需要的工具类,而且每个工具类是相互解耦的.下面,我们就来一起看看它的集成及使用. 首先,我们看一下集成方式 下载jar包导入1.下载最新jar包Utils.jar-v1.32

常用的自定义和官方工具类

生成唯一数: 1 public static synchronized String generateUniqueID() { 2 if (seq > ROTATION) 3 seq = 0; 4 buf.delete(0, buf.length()); 5 date.setTime(System.currentTimeMillis()); 6 // 年月日时分秒 + 自增的五位十进制数 7 String str = String.format("%1$tY%1$tm%1$td%1$tk%