Android自定義Dialog


Android開發過程中,常常會遇到一些需求場景——在界面上彈出一個彈框,對用戶進行提醒並讓用戶進行某些選擇性的操作,

如退出登錄時的彈窗,讓用戶選擇“退出”還是“取消”等操作。

Android系統提供了Dialog類,以及Dialog的子類,常見如AlertDialog來實現此類功能。

一般情況下,利用Android提供的Dialog及其子類能夠滿足多數此類需求,然而,其不足之處體現在:

1. 基於Android提供的Dialog及其子類樣式單一,風格上與App本身風格可能不太協調;

2. Dialog彈窗在布局和功能上有所限制,有時不一定能滿足實際的業務需求。

本文將通過在Dialog基礎上構建自定義的Dialog彈窗,以最常見的確認彈框為例。

本樣式相對比較簡單:上面有一個彈框標題(提示語),下面左右分別是“確認”和“取消”按鈕,當用戶點擊“確認”按鈕時,彈框執行

相應的確認邏輯,當點擊“取消”按鈕時,執行相應的取消邏輯。

首先,自定義彈框樣式:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3  android:layout_width="match_parent"
 4  android:layout_height="wrap_content"
 5  android:background="@drawable/dialog_bg"
 6  android:orientation="vertical" >
 7 
 8     <TextView  9         android:id="@+id/title"
10  android:layout_width="wrap_content"
11  android:layout_height="wrap_content"
12  android:layout_gravity="center"
13  android:paddingTop="14dp"
14  android:textColor="@color/login_hint"
15  android:textSize="@dimen/text_size_18" />
16 
17     <LinearLayout 18         android:layout_width="match_parent"
19  android:layout_height="wrap_content"
20  android:layout_marginBottom="14dp"
21  android:layout_marginLeft="20dp"
22  android:layout_marginRight="20dp"
23  android:layout_marginTop="30dp" >
24 
25         <TextView 26             android:id="@+id/confirm"
27  android:layout_width="wrap_content"
28  android:layout_height="wrap_content"
29  android:layout_marginRight="10dp"
30  android:layout_weight="1"
31  android:background="@drawable/btn_confirm_selector"
32  android:gravity="center"
33  android:textColor="@color/white"
34  android:textSize="@dimen/text_size_16" />
35 
36         <TextView 37             android:id="@+id/cancel"
38  android:layout_width="wrap_content"
39  android:layout_height="wrap_content"
40  android:layout_marginLeft="10dp"
41  android:layout_weight="1"
42  android:background="@drawable/btn_cancel_selector"
43  android:gravity="center"
44  android:textColor="@color/login_hint"
45  android:textSize="@dimen/text_size_16" />
46     </LinearLayout>
47 
48 </LinearLayout>

 

然后,通過繼承Dialog類構建確認彈框控件ConfirmDialog:

 1 package com.corn.widget;  2 
 3 import android.app.Dialog;  4 import android.content.Context;  5 import android.os.Bundle;  6 import android.util.DisplayMetrics;  7 import android.view.LayoutInflater;  8 import android.view.View;  9 import android.view.Window; 10 import android.view.WindowManager; 11 import android.widget.TextView; 12 
13 import com.corn.R; 14 
15 public class ConfirmDialog extends Dialog { 16 
17     private Context context; 18     private String title; 19     private String confirmButtonText; 20     private String cacelButtonText; 21     private ClickListenerInterface clickListenerInterface; 22 
23     public interface ClickListenerInterface { 24 
25         public void doConfirm(); 26 
27         public void doCancel(); 28  } 29 
30     public ConfirmDialog(Context context, String title, String confirmButtonText, String cacelButtonText) { 31         super(context, R.style.MyDialog); 32         this.context = context; 33         this.title = title; 34         this.confirmButtonText = confirmButtonText; 35         this.cacelButtonText = cacelButtonText; 36  } 37 
38  @Override 39     protected void onCreate(Bundle savedInstanceState) { 40         // TODO Auto-generated method stub
41         super.onCreate(savedInstanceState); 42 
43  init(); 44  } 45 
46     public void init() { 47         LayoutInflater inflater = LayoutInflater.from(context); 48         View view = inflater.inflate(R.layout.confirm_dialog, null); 49  setContentView(view); 50 
51         TextView tvTitle = (TextView) view.findViewById(R.id.title); 52         TextView tvConfirm = (TextView) view.findViewById(R.id.confirm); 53         TextView tvCancel = (TextView) view.findViewById(R.id.cancel); 54 
55  tvTitle.setText(title); 56  tvConfirm.setText(confirmButtonText); 57  tvCancel.setText(cacelButtonText); 58 
59         tvConfirm.setOnClickListener(new clickListener()); 60         tvCancel.setOnClickListener(new clickListener()); 61 
62         Window dialogWindow = getWindow(); 63         WindowManager.LayoutParams lp = dialogWindow.getAttributes(); 64         DisplayMetrics d = context.getResources().getDisplayMetrics(); // 獲取屏幕寬、高用
65         lp.width = (int) (d.widthPixels * 0.8); // 高度設置為屏幕的0.6
66  dialogWindow.setAttributes(lp); 67  } 68 
69     public void setClicklistener(ClickListenerInterface clickListenerInterface) { 70         this.clickListenerInterface = clickListenerInterface; 71  } 72 
73     private class clickListener implements View.OnClickListener { 74  @Override 75         public void onClick(View v) { 76             // TODO Auto-generated method stub
77             int id = v.getId(); 78             switch (id) { 79             case R.id.confirm: 80  clickListenerInterface.doConfirm(); 81                 break; 82             case R.id.cancel: 83  clickListenerInterface.doCancel(); 84                 break; 85  } 86  } 87 
88  }; 89 
90 }

在如上空間構造代碼中,由於控件的"確認"和"取消"邏輯與實際的應用場景有關,因此,控件中通過定義內部接口來實現。

 

在需要使用此控件的地方,進行如下形式調用:

 1 public static void Exit(final Context context) {  2         final ConfirmDialog confirmDialog = new ConfirmDialog(context, "確定要退出嗎?", "退出", "取消");  3  confirmDialog.show();  4         confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {  5  @Override  6             public void doConfirm() {  7                 // TODO Auto-generated method stub
 8  confirmDialog.dismiss();  9                 //toUserHome(context);
10  AppManager.getAppManager().AppExit(context); 11  } 12 
13  @Override 14             public void doCancel() { 15                 // TODO Auto-generated method stub
16  confirmDialog.dismiss(); 17  } 18  }); 19     }

調用中實現了此控件的內部接口,並賦給控件本身,以此在點擊按鈕時實現基於外部具體業務邏輯的函數回調。

 


免責聲明!

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



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