Android—關於自定義對話框的工具類

来源:http://www.cnblogs.com/yunfang/archive/2016/05/31/5548010.html
-Advertisement-
Play Games

開發中有很多地方會用到自定義對話框,為了避免不必要的城府代碼,在此總結出一個工具類。 彈出對話框的地方很多,但是都大同小異,不同無非就是提示內容或者圖片不同,下麵這個類是將提示內容和圖片放到了自定義函數的參數中,並且是靜態,可以用類直接調用此函數。 對話框的xml文件佈局: Activity中彈出對 ...


開發中有很多地方會用到自定義對話框,為了避免不必要的城府代碼,在此總結出一個工具類。

彈出對話框的地方很多,但是都大同小異,不同無非就是提示內容或者圖片不同,下麵這個類是將提示內容和圖片放到了自定義函數的參數中,並且是靜態,可以用類直接調用此函數。

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()方法,並將上下文,自定義的內容和圖片的資源傳遞到參數中從而得到需要的效果。

上圖:

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 效果一: 1.首先,整個底部懸浮通欄廣告是固定在瀏覽器的底部,隨著瀏覽器的滾動,底部懸浮廣告始終在瀏覽器視窗中。這裡有幾個關鍵點:通欄,固定,黑色。 所以:首先我們必須給懸浮通欄廣告整體一個100%的寬度,其次給它設定固定定位,固定在瀏覽器底部,背景色為黑色,透明度為0.7。 2. 底部懸浮通欄廣告 ...
  • <!DOCTYPE html><html lang="zh-CN"><head> <meta charset="UTF-8"> <title>GetUserMedia實例</title></head><body> <video id="video" autoplay><ideo></body><sc ...
  • 今天比較閑所以就花點時間又寫了點東西。 相信這種價格表大家不會陌生 現在我就模仿它做一個簡單版本的。效果如下 首先需要兩個時間控制項,我這裡用的是HTML5裡面的時間控制項,這個沒限制喜歡用什麼就用什麼 直接上代碼吧!我都寫了註釋。 ...
  • 如題: ...
  • [Bootstrap](http://hovertree.com/menu/bootstrap/) 是由兩個 twitter 員工開發並開源的前端框架,非常火爆,而如此火爆自然有它的道理,在我們團隊的所有項目中正全面推行使用 Bootstrap,我想根據自己的實際使用體驗來說明一下為什麼要用 Boo ...
  • 在前端領域里,很多時候,一個簡單的功能可以有很多種不同的實現方式,今天就拿星期幾的不同腳本實現方法作為例子,希望能激發童鞋們更多的想法。 1、使用 if 語句: 2、使用switch case 語句: 3、神器,一句話解決: ...
  • 說說我自己的思路 首先需要一個初始div 然後就是滑鼠事件,想象一下拖動這個過程需要用到哪些事件。。。 1、滑鼠按鈕按下事件mousedown 2、滑鼠按鈕鬆開事件mouseup 3、滑鼠移動事件mousemove 第一步:給div添加滑鼠按鈕按下事件,這個事件需要完成以下事情 一、獲取div到瀏覽 ...
  • Fragment使用的基本知識點總結, 包括Fragment的添加, 參數傳遞和通信, 生命周期和各種操作. ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...