1. 程式人生 > >自定義Dialog的詳細步驟

自定義Dialog的詳細步驟

sset als java 事件 and protect 數據 獲取 是不是

自定義Dialog的詳細步驟(實現自定義樣式一般原理)

現在很多App的提示對話框都非常有個性,然而你還用系統的對話框樣式,是不是覺得很落後呢,今天我就給大家講講怎樣自定義自己的Dialog,學會了之後,你就會根據自家app的主題,設計出相應的Dialog的風格。

好了接下來我就以一個簡單風格的自定義Dialog來講講自定義dialog的一般步驟和原理。

第一步: 給Dialog設置一個風格主題(基本都是用這個主題)無邊框全透明背景

[html] view plain copy 技術分享技術分享
  1. <!--自定義dialog背景全透明無邊框theme -->
  2. <style name="MyDialog" parent="android:style/Theme.Dialog">
  3. <!--背景顏色及和透明程度-->
  4. <item name="android:windowBackground">@android:color/transparent</item>
  5. <!--是否去除標題 -->
  6. <item name="android:windowNoTitle">true</item>
  7. <!--是否去除邊框-->
  8. <item name="android:windowFrame">@null</item>
  9. <!--是否浮現在activity之上-->
  10. <item name="android:windowIsFloating">true</item>
  11. <!--是否模糊-->
  12. <item name="android:backgroundDimEnabled">false</item>
  13. </style>

第二步:給自定的Dialog設置自定義的 xml界面,我這裏只是示範,你可以使用單選,多選,3個按鈕,4個按鈕等等,格式各樣的自定義XML,我這裏就定義了 標題title,信息message,還有一個確定按鈕和取消按鈕,如下:

[html] view plain copy 技術分享技術分享
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#11ffffff">
  6. <LinearLayout
  7. android:layout_width="260dp"
  8. android:layout_height="wrap_content"
  9. android:layout_centerInParent="true"
  10. android:background="@drawable/free_dialog_bg"
  11. android:orientation="vertical">
  12. <TextView
  13. android:id="@+id/title"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_gravity="center"
  17. android:layout_margin="15dp"
  18. android:gravity="center"
  19. android:text="消息提示"
  20. android:textColor="#38ADFF"
  21. android:textSize="16sp" />
  22. <TextView
  23. android:id="@+id/message"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:layout_marginLeft="20dp"
  27. android:layout_marginRight="20dp"
  28. android:text="提示消息" />
  29. <View
  30. android:layout_width="match_parent"
  31. android:layout_height="1px"
  32. android:layout_marginTop="15dp"
  33. android:background="#E4E4E4" />
  34. <LinearLayout
  35. android:layout_width="match_parent"
  36. android:layout_height="40dp"
  37. android:orientation="horizontal">
  38. <Button
  39. android:id="@+id/no"
  40. android:layout_width="0dp"
  41. android:layout_height="match_parent"
  42. android:layout_marginLeft="10dp"
  43. android:layout_weight="1"
  44. android:background="@null"
  45. android:gravity="center"
  46. android:singleLine="true"
  47. android:text="No"
  48. android:textColor="#7D7D7D"
  49. android:textSize="16sp" />
  50. <View
  51. android:layout_width="1px"
  52. android:layout_height="match_parent"
  53. android:background="#E4E4E4" />
  54. <Button
  55. android:id="@+id/yes"
  56. android:layout_width="0dp"
  57. android:layout_height="match_parent"
  58. android:layout_marginRight="10dp"
  59. android:layout_weight="1"
  60. android:background="@null"
  61. android:gravity="center"
  62. android:singleLine="true"
  63. android:text="Yes"
  64. android:textColor="#38ADFF"
  65. android:textSize="16sp" />
  66. </LinearLayout>
  67. </LinearLayout>
  68. </RelativeLayout>


dialog的自定義背景框如下:

[html] view plain copy 技術分享技術分享
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">
  3. <solid android:color="#ffffff" />
  4. <stroke
  5. android:width="0.8dp"
  6. android:color="#ffffff" />
  7. <!-- 圓角 -->
  8. <corners android:radius="6dp" />
  9. </shape>

最後的樣子如下,紅框中的部分( 非常簡潔,但是市面上很多app都使用這個樣式):

技術分享

第三步:繼承Dialog實現自定義的Dialog,先上代碼,供大家欣賞下:

[java] view plain copy 技術分享技術分享
  1. package com.world.hello.selfdialog;
  2. import android.app.Dialog;
  3. import android.content.Context;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.TextView;
  8. /**
  9. * 創建自定義的dialog,主要學習其實現原理
  10. * Created by chengguo on 2016/3/22.
  11. */
  12. public class SelfDialog extends Dialog {
  13. private Button yes;//確定按鈕
  14. private Button no;//取消按鈕
  15. private TextView titleTv;//消息標題文本
  16. private TextView messageTv;//消息提示文本
  17. private String titleStr;//從外界設置的title文本
  18. private String messageStr;//從外界設置的消息文本
  19. //確定文本和取消文本的顯示內容
  20. private String yesStr, noStr;
  21. private onNoOnclickListener noOnclickListener;//取消按鈕被點擊了的監聽器
  22. private onYesOnclickListener yesOnclickListener;//確定按鈕被點擊了的監聽器
  23. /**
  24. * 設置取消按鈕的顯示內容和監聽
  25. *
  26. * @param str
  27. * @param onNoOnclickListener
  28. */
  29. public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
  30. if (str != null) {
  31. noStr = str;
  32. }
  33. this.noOnclickListener = onNoOnclickListener;
  34. }
  35. /**
  36. * 設置確定按鈕的顯示內容和監聽
  37. *
  38. * @param str
  39. * @param onYesOnclickListener
  40. */
  41. public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {
  42. if (str != null) {
  43. yesStr = str;
  44. }
  45. this.yesOnclickListener = onYesOnclickListener;
  46. }
  47. public SelfDialog(Context context) {
  48. super(context, R.style.MyDialog);
  49. }
  50. @Override
  51. protected void onCreate(Bundle savedInstanceState) {
  52. super.onCreate(savedInstanceState);
  53. setContentView(R.layout.free_exercise_sure_dialog_layout);
  54. //按空白處不能取消動畫
  55. setCanceledOnTouchOutside(false);
  56. //初始化界面控件
  57. initView();
  58. //初始化界面數據
  59. initData();
  60. //初始化界面控件的事件
  61. initEvent();
  62. }
  63. /**
  64. * 初始化界面的確定和取消監聽器
  65. */
  66. private void initEvent() {
  67. //設置確定按鈕被點擊後,向外界提供監聽
  68. yes.setOnClickListener(new View.OnClickListener() {
  69. @Override
  70. public void onClick(View v) {
  71. if (yesOnclickListener != null) {
  72. yesOnclickListener.onYesClick();
  73. }
  74. }
  75. });
  76. //設置取消按鈕被點擊後,向外界提供監聽
  77. no.setOnClickListener(new View.OnClickListener() {
  78. @Override
  79. public void onClick(View v) {
  80. if (noOnclickListener != null) {
  81. noOnclickListener.onNoClick();
  82. }
  83. }
  84. });
  85. }
  86. /**
  87. * 初始化界面控件的顯示數據
  88. */
  89. private void initData() {
  90. //如果用戶自定了title和message
  91. if (titleStr != null) {
  92. titleTv.setText(titleStr);
  93. }
  94. if (messageStr != null) {
  95. messageTv.setText(messageStr);
  96. }
  97. //如果設置按鈕的文字
  98. if (yesStr != null) {
  99. yes.setText(yesStr);
  100. }
  101. if (noStr != null) {
  102. no.setText(noStr);
  103. }
  104. }
  105. /**
  106. * 初始化界面控件
  107. */
  108. private void initView() {
  109. yes = (Button) findViewById(R.id.yes);
  110. no = (Button) findViewById(R.id.no);
  111. titleTv = (TextView) findViewById(R.id.title);
  112. messageTv = (TextView) findViewById(R.id.message);
  113. }
  114. /**
  115. * 從外界Activity為Dialog設置標題
  116. *
  117. * @param title
  118. */
  119. public void setTitle(String title) {
  120. titleStr = title;
  121. }
  122. /**
  123. * 從外界Activity為Dialog設置dialog的message
  124. *
  125. * @param message
  126. */
  127. public void setMessage(String message) {
  128. messageStr = message;
  129. }
  130. /**
  131. * 設置確定按鈕和取消被點擊的接口
  132. */
  133. public interface onYesOnclickListener {
  134. public void onYesClick();
  135. }
  136. public interface onNoOnclickListener {
  137. public void onNoClick();
  138. }
  139. }

主actvitiy的代碼如下:

[java] view plain copy 技術分享技術分享
  1. package com.world.hello.selfdialog;
  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;
  5. import android.widget.Toast;
  6. public class MainActivity extends AppCompatActivity {
  7. private SelfDialog selfDialog;
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. findViewById(R.id.self_dialog).setOnClickListener(new View.OnClickListener() {
  13. @Override
  14. public void onClick(View v) {
  15. selfDialog = new SelfDialog(MainActivity.this);
  16. selfDialog.setTitle("提示");
  17. selfDialog.setMessage("確定退出應用?");
  18. selfDialog.setYesOnclickListener("確定", new SelfDialog.onYesOnclickListener() {
  19. @Override
  20. public void onYesClick() {
  21. Toast.makeText(MainActivity.this,"點擊了--確定--按鈕",Toast.LENGTH_LONG).show();
  22. selfDialog.dismiss();
  23. }
  24. });
  25. selfDialog.setNoOnclickListener("取消", new SelfDialog.onNoOnclickListener() {
  26. @Override
  27. public void onNoClick() {
  28. Toast.makeText(MainActivity.this,"點擊了--取消--按鈕",Toast.LENGTH_LONG).show();
  29. selfDialog.dismiss();
  30. }
  31. });
  32. selfDialog.show();
  33. }
  34. });
  35. }
  36. }

下面是gif示例圖:

技術分享

點擊打開鏈接下載實例,免積分

好了,看了上面的例子,大家是不是覺得,以後都可以自定義自己的dialog了呢,這裏在給大家講講具體原理:

就拿本例來說,我定義了一個title,一個message,一個確定按鈕,一個取消按鈕。

1、通過構造方法給dialog設置一個主題 R.style.MyDialog , 主要設置dialog的顯示屬性,一般都是 全透明無邊框 ;

2、然後在dialog的onCreate()方法中,用setContentView( R.layout.SelfDialog) 為dialog設置XML文件,我們就可以在layout文件中創建自定義的Dialog風格。這裏我就自定義了xml文件格式,實現了自定義的外觀風格,不受系統的主題影響。

3、然後通過設置要為外界設置一些public 公開的方法,來向自定義的dialog傳遞值。這裏的title 和 message,都是可以通過外界傳值進來,進行設置的。如下面的public 方法就是供外界activity來設置title和message的:

[java] view plain copy 技術分享技術分享
  1. /**
  2. * 從外界Activity為Dialog設置標題
  3. *
  4. * @param title
  5. */
  6. public void setTitle(String title) {
  7. titleStr = title;
  8. }
  9. /**
  10. * 從外界Activity為Dialog設置dialog的message
  11. *
  12. * @param message
  13. */
  14. public void setMessage(String message) {
  15. messageStr = message;
  16. }

在activity通過實例化Dialog後就可以設置titile和message了。

[java] view plain copy 技術分享技術分享
  1. selfDialog = new SelfDialog(MainActivity.this);
  2. selfDialog.setTitle("提示");
  3. selfDialog.setMessage("確定退出應用?");

4、最後,自定義的dialog中包含了一些按鈕的時候,這個時候要想讓按鈕有點擊事件,並且把這個點擊事件能夠傳遞給activity,讓acitvity做一些事情,這裏就需要設置監聽接口,讓button的點擊事件能夠讓外界activity知道。如下面的代碼。

[java] view plain copy 技術分享技術分享
  1. /**
  2. * 設置確定按鈕和取消被點擊的接口
  3. */
  4. public interface onYesOnclickListener {
  5. public void onYesClick();
  6. }
  7. public interface onNoOnclickListener {
  8. public void onNoClick();
  9. }
[java] view plain copy 技術分享技術分享
  1. private onNoOnclickListener noOnclickListener;//取消按鈕被點擊了的監聽器
  2. private onYesOnclickListener yesOnclickListener;//確定按鈕被點擊了的監聽器
  3. /**
  4. * 設置取消按鈕的顯示內容和監聽
  5. *
  6. * @param str
  7. * @param onNoOnclickListener
  8. */
  9. public void setNoOnclickListener(String str, onNoOnclickListener onNoOnclickListener) {
  10. if (str != null) {
  11. noStr = str;
  12. }
  13. this.noOnclickListener = onNoOnclickListener;
  14. }
  15. /**
  16. * 設置確定按鈕的顯示內容和監聽
  17. *
  18. * @param str
  19. * @param onYesOnclickListener
  20. */
  21. public void setYesOnclickListener(String str, onYesOnclickListener onYesOnclickListener) {
  22. if (str != null) {
  23. yesStr = str;
  24. }
  25. this.yesOnclickListener = onYesOnclickListener;
  26. }
[java] view plain copy 技術分享技術分享
  1. //設置確定按鈕被點擊後,向外界提供監聽
  2. yes.setOnClickListener(new View.OnClickListener() {
  3. @Override
  4. public void onClick(View v) {
  5. if (yesOnclickListener != null) {
  6. yesOnclickListener.onYesClick();
  7. }
  8. }
  9. });
  10. //設置取消按鈕被點擊後,向外界提供監聽
  11. no.setOnClickListener(new View.OnClickListener() {
  12. @Override
  13. public void onClick(View v) {
  14. if (noOnclickListener != null) {
  15. noOnclickListener.onNoClick();
  16. }
  17. }
  18. });


activity就可以設置監聽接口來實時獲取button的點擊事件如下:

[java] view plain copy 技術分享技術分享
  1. selfDialog.setYesOnclickListener("確定", new SelfDialog.onYesOnclickListener() {
  2. @Override
  3. public void onYesClick() {
  4. Toast.makeText(MainActivity.this,"點擊了--確定--按鈕",Toast.LENGTH_LONG).show();
  5. selfDialog.dismiss();
  6. }
  7. });
  8. selfDialog.setNoOnclickListener("取消", new SelfDialog.onNoOnclickListener() {
  9. @Override
  10. public void onNoClick() {
  11. Toast.makeText(MainActivity.this,"點擊了--取消--按鈕",Toast.LENGTH_LONG).show();
  12. selfDialog.dismiss();
  13. }
  14. });

通過上面4步的詳細講解,就能知道自定義dialog的一般原理,基本就是 要為dialog設置一些基本的文字信息時,就直接用公開方法public 的方法,讓外界直接設置;如果要讓activity監聽到button之類的點擊事件就自定義接口,用監聽接口的方式向activity傳遞點擊事件。

自定義Dialog的詳細步驟