[android]我是這樣寫自定義Dialog的


1、前言#

然而就先特么扯個犢子…#####
* 其實,關于android的自定義dialog網絡上很多文章。我也不知道我這篇會不會特別點~
  小渣渣我只想寫點個人感覺有意義的東西。
(其實也是不知道寫啥啦,歡迎大家有想了解的知識點、或者想法可以私信或  者留言給本渣~
  個人感覺有意義的有興趣的就寫出來~hhhhhh  )
 
  不扯犢子了…感覺扯犢子會被打…

2、概述

  • 百度上各種自定義對話框很多,可是感覺都好亂呀呀呀呀。之前寫自定義對話框的代碼總是巴拉到一團,臥槽……看著就煩,改起來更煩…對于近視度數極高的我來說實在是受不了(額,雖然好像和近視沒啥關系…別在意這了)

所以,本文的目的就是騙關注的…個屁,就是介紹個人一個寫自定義Dialog的方式。

大概就是:

  • 提高可維護性.
  • 代碼結構思路清晰點,不會像狗皮膏藥黏在一團.
  • 個人感覺可控性蠻高.
  • 然后自己感覺下吧……
    (優缺點什么的,都是自己體會下的好,我目前所知道的好方式不一定是最好的方式,要不然我怎么自稱本渣渣呢,畢竟牛在天上飛,我在地上吹~)

3、思路#

首先,dialog這個玩意兒我們還是很經常用到的,不同的需求可能涉及不一樣的dialog,所以咱寫一個自定義dialog的父類,父類就一個很普通dialog嘛,然后,為人基本準則給你了,兒子你愛咋整咋整吧。恩的,把兒子放養去~你啥功能自己搞去,爹爹我只要你還是個人(dialog)就好了!

就按著這個思路咱敲代碼去!
示例,一個支付時需要輸入密碼的一個對話框,需要顯示交易金額,輸入密碼框。

效果圖:#####
自定義Dialog.gif

4、代碼

*前面說的都是打屁,還挺悠長的言歸正傳代碼走起來

1、首先按照思路,我們不能像平常寫自定義Dialog一般,一股腦的吧嗒吧嗒的鞋寫出來.所以先寫一個Dialog的父類。

☆☆☆☆☆

  • 既然是父類,所以我們新建個抽象類。我這邊就取名叫做BaseDialog了.(其實寫完這個基本類這篇文章就GG了)
public abstract class BaseDialog {

//這些屬性,Context 是肯定要的,基本對話框要用它
protected Context context;
private Display display;//這個設置顯示屬性用的 
private Dialog dialog;//自定義Dialog,Dialog還是要有一個的吧
   
//對話框布局的樣式ID (通過這個抽象方法,我們可以給不同的對話框設置不同樣式主題)
 protected abstract int getDialogStyleId();
//構建對話框的方法(都說了是不同的對話框,布局什么的肯定是不一樣的)
protected abstract View getView();

//構造方法 來實現 最基本的對話框
public BaseDialog(Context context) {  
     this.context = context;
     WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);  
     display = windowManager.getDefaultDisplay();
     //在這里初始化 基礎對話框s    
     if (getDialogStyleId() == 0){     
           dialog = new Dialog(context, DIALOG_COMMON_STYLE );   
     }else {       
           dialog = new Dialog(context, getDialogStyleId());    
     }    
     // 調整dialog背景大小    
     getView().setLayoutParams(new FrameLayout.LayoutParams((int) (display.getWidth() * 0.8), LinearLayout.LayoutParams.WRAP_CONTENT));    
dialog.setContentView(getView());    
     //隱藏系統輸入盤 
     dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  }

   /** * Dialog 的基礎方法,
    *凡是要用的就在這寫出來,然后直接用對話框調本來的方法就好了,不夠自己加~hhh */
  
  //像這類設置對話框屬性的方法,就返回值寫自己,這樣就可以一條鏈式設置了
  public BaseDialog setCancelable(boolean cancel) {  
     dialog.setCancelable(cancel);   
     return this;
   }

   public void show() {    
     dialog.show();
  }

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

   public boolean isShowing(){ 
          return dialog.isShowing();
   }

  public BaseDialog setdismissListeren(DialogInterface.OnDismissListener dismissListener){    
      dialog.setOnDismissListener(dismissListener);    
      return this;
   }

}
  • 上面有倆個樣式,樣式的話寫在res/values/styles.xml中.
<!-- 對話框樣式(要什么樣式自己網上找去…應該沒有比我懶得吧) : --><!-- 自定義仿IOS的AlertDialog的樣式 -->
<style name="dialog_ios_style" parent="@android:style/Theme.Dialog">   
<item name="android:windowBackground">@android:color/transparent</item>    
<item name="android:windowContentOverlay">@null</item>    
<item name="android:windowIsFloating">true</item>  
<item name="android:windowFrame">@null</item>    
<item name="android:backgroundDimEnabled">true</item>  
<item name="android:windowNoTitle">true</item>    
<item name="android:windowIsTranslucent">true</item>
</style>

<!-- 一個很正常的樣式 -->
<style name="common_dialog_style" parent="@android:style/Theme.Dialog">    
<item name="android:windowBackground">@android:color/transparent</item>    
<item name="android:windowFrame">@null</item>    
<item name="android:windowNoTitle">true</item>    
<item name="android:windowIsFloating">true</item>    
<item name="android:windowIsTranslucent">true</item>    
<item name="android:windowContentOverlay">@null</item>    
<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>    
<item name="android:backgroundDimEnabled">true</item>
</style>

*最重要的父類就寫好了然后,所有的自定義對話框繼承他,實現自己的方法就好了~~

2、然后咱寫一個自定義的支付確認對話框.(這只是個例子,愛咋玩,自己寫去,小渣渣比較懶。。就舉這一個例子啦QAQ,其實覺得舉太多也沒什么卵用)
  • 按照上面說的寫個類繼承剛剛寫的BaseDialog
public class PassWordDialog extends BaseDialog {

    //這些屬性無視就好了,就是布局的控件
     private TextView btnDialogPwdTitle;
     private TextView tvDialogPwdTitle;
     private LinearLayout layoutDialogPwdMoney;
     private EditText etDialogPwd;
     private LinearLayout layoutDialogPwd;
     private RelativeLayout layoutPwd;
     private TextView tvDialogPwdMoney;

      //構造方法還是要的哈  
     public PassWordDialog(Context context) {    super(context);}

     //設置對話框的樣式
     @Override
     protected int getDialogStyleId() {    
          return BaseDialog.DIALOG_COMMON_STYLE;
     }

    //繼承于BaseDialog的方法,設置布局用的,這樣對話框張啥樣久隨心所欲啦
    @Override
    protected View getView() {    
       // 獲取Dialog布局    
       View view = LayoutInflater.from(context).inflate(R.layout.dialog_pwd, null);   
    
       //得到各種view
       layoutPwd= ViewUtility.findViewById(view,R.id.rl_dialog_pwd);    
       tvDialogPwdMoney = ViewUtility.findViewById(view, R.id.tv_dialog_pwd_money);    
       btnDialogPwdTitle = ViewUtility.findViewById(view,R.id.btn_dialog_pwd_title);    
       tvDialogPwdTitle = ViewUtility.findViewById(view,R.id.tv_dialog_pwd_title);    
       layoutDialogPwdMoney = ViewUtility.findViewById(view,R.id.layout_dialog_pwd_money);    
       etDialogPwd = ViewUtility.findViewById(view,R.id.et_dialog_pwd);    
        layoutDialogPwd = ViewUtility.findViewById(view,R.id.layout_dialog_pwd);    
        //初始化一些控件的方法(放下面寫啦~)
        initViewEvent();    
        return view;
     }
}
  • 上面這個類需要的元素在補一下。一個是布局文件(怎么排版看個人心意了),一個是初始化一些個人覺得所需要的控件監聽.
  1. 先貼下監聽事件的方法.
//View的事件
private void initViewEvent() {
     //設置對話框那個叉叉的方法,點擊關閉對話框
     btnDialogPwdTitle.setOnClickListener(new View.OnClickListener() {    
        @Override        
        public void onClick(View v) { 
              dismiss();        
        }    
     });
 }
  1. 可能需求是,需要根據用戶輸入的金額在對話框顯示支付金額,好的,so easy那就給個設置金額的方法.
 /** * 設置金額 */
 public BaseDialog setMoney(String s){ 
      tvDialogPwdMoney.setText(s);    
      return this;
 }
  1. 如果實在需要用到對話框里的控件.
/** * 得到密碼框 */
public EditText getEditText(){    
      return etDialogPwd;
}
  1. 然后……想咋玩?你說,然后該咋整咋整好不.

  2. 最后貼個布局文件.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout    
xmlns:android="http://schemas.android.com/apk/res/android"    
android:id="@+id/rl_dialog_pwd"    
android:orientation="vertical"    
android:layout_width="match_parent"    
android:layout_height="wrap_content"    
android:background="#ffffff"    
>    <!-- 取消對話框按鈕圖標 -->    
<TextView        
android:id="@+id/btn_dialog_pwd_title"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:text="X"        
android:textSize="30sp"       
android:layout_margin="10dp"        />   
<TextView        
android:id="@+id/tv_dialog_pwd_title"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"       
android:text="請輸入支付密碼"        
android:textSize="20sp"        
android:layout_margin="18dp"        
android:layout_toRightOf="@id/btn_dialog_pwd_title"        />    
<!-- 修飾線-->    
<View        
android:layout_width="match_parent"        
android:layout_height="1dp"        
android:background="#ff0000"        
android:layout_below="@id/btn_dialog_pwd_title"        />    
<LinearLayout        
android:id="@+id/layout_dialog_pwd_money"        
android:layout_width="match_parent"        
android:layout_height="100dp"        
android:layout_below="@id/btn_dialog_pwd_title"        
android:gravity="center"        >        
<TextView            
android:layout_width="wrap_content"            
android:layout_height="wrap_content"            
android:text="¥"            
android:textSize="40sp"            
android:layout_gravity="center_vertical"            />        
<TextView            
android:id="@+id/tv_dialog_pwd_money"            
android:layout_width="wrap_content"            
android:layout_height="wrap_content"            
android:text="40.95"            
android:textSize="40sp"            
android:layout_gravity="center_vertical"            />    
</LinearLayout>    
<!-- 修飾線-->    
<View        
android:layout_width="match_parent"        
android:layout_height="1dp"        
android:background="#aaaaaa"        
android:layout_marginLeft="20dp"        
android:layout_marginRight="20dp"        
android:layout_below="@id/layout_dialog_pwd_money"        />    
<!-- 密碼輸入框-->    
<LinearLayout        
android:id="@+id/layout_dialog_pwd"        
android:layout_width="match_parent"        
android:layout_height="80dp"        
android:layout_below="@id/layout_dialog_pwd_money"        >        
<EditText            
android:id="@+id/et_dialog_pwd"            
android:layout_width="match_parent"            
android:layout_height="wrap_content"            
android:background="@android:drawable/edit_text"            
android:layout_gravity="center"            
android:layout_marginLeft="10dp"            
android:layout_marginRight="10dp"            
android:inputType="numberPassword"            
android:longClickable="false"            
android:maxLength="6"            
android:clickable="false"            />    
</LinearLayout>
</RelativeLayout>

4、引用


* 對話框已經寫好了,調用方面那就真JB爽歪了.
public void showDialog(View view){     
       //恩,對的,就這么一行代碼就夠了,因為設置屬性的方法返回值是自己,所以一條鏈就點好了
       new PassWordDialog(this).setMoney("10").setCancelable(false).show();
 }

5、這作者真煩,啰嗦一大堆終于講完了~

※然而,我還想啰嗦下!咬我呀!

路人葵:希望可幫到有需要的小伙伴們~
那啥…有想研究的代碼呀,功能呀,建議呀,可以評論留言私信~
雖然太高深的也不會,可這不正在學么~
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容