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

歡迎提意見,希望可以給大家帶來幫助~


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM