Android中Dialog簡(jiǎn)析

均屬于筆記,僅供個(gè)人參考,有問題歡迎指正,整理模式

一,7種形式的Android Dialog使用舉例

在Android開發(fā)中,我們經(jīng)常會(huì)需要在Android界面上彈出一些對(duì)話框,比如詢問用戶或者讓用戶選擇。這些功能我們叫它Android Dialog對(duì)話框,在我們使用Android的過程中,我歸納了一下,Android Dialog的類型無非也就7種,下面我分別向大家介紹這7種Android Dialog對(duì)話框的使用方法,希望對(duì)大家能有所幫助。

1.該效果是當(dāng)按返回按鈕時(shí)彈出一個(gè)提示,來確保無誤操作,采用常見的對(duì)話框樣式。

創(chuàng)建dialog對(duì)話框方法代碼如下:

創(chuàng)建dialog對(duì)話框方法代碼如下:

protected void dialog() {?

  ? AlertDialog.Builder builder = new Builder(Main.this);?

  ? builder.setMessage("確認(rèn)退出嗎?");?

  ? builder.setTitle("提示");?

  ? builder.setPositiveButton("確認(rèn)", new OnClickListener() {?

  ? @Override?

  ? public void onClick(DialogInterface dialog, int which) {?

  ? ? dialog.dismiss();?

  ? ? Main.this.finish();?

  ? }?

  ? });?

  ? builder.setNegativeButton("取消", new OnClickListener() {?

  ? @Override?

  ? public void onClick(DialogInterface dialog, int which) {?

  ? ? dialog.dismiss();?

  ? }?

  ? });?

  ? builder.create().show();?

   }?

在onKeyDown(int keyCode, KeyEvent event)方法中調(diào)用此方法

if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {?

  ? dialog();?

  ? }?

  ? return false;?

   }?

2.改變了對(duì)話框的圖表,添加了三個(gè)按鈕

創(chuàng)建dialog的方法代碼如下:

Dialog dialog = new AlertDialog.Builder(this).setIcon(?

  ? ? android.R.drawable.btn_star).setTitle("喜好調(diào)查").setMessage(?

  ? ? "你喜歡李連杰的電影嗎?").setPositiveButton("很喜歡",?

  ? ? new OnClickListener() {?

  ? ? ? @Override?

  ? ? ? public void onClick(DialogInterface dialog, int which) {?

  ? ? ? // TODO Auto-generated method stub?

  ? ? ? Toast.makeText(Main.this, "我很喜歡他的電影。",?

  ? ? ? ? Toast.LENGTH_LONG).show();?

  ? ? ? }?

  ? ? }).setNegativeButton("不喜歡", new OnClickListener() {?

  ? ? @Override?

  ? ? public void onClick(DialogInterface dialog, int which) {?

  ? ? // TODO Auto-generated method stub?

  ? ? Toast.makeText(Main.this, "我不喜歡他的電影。", Toast.LENGTH_LONG)?

  ? ? ? .show();?

  ? ? }?

  ? }).setNeutralButton("一般", new OnClickListener() {?

  ? ? @Override?

  ? ? public void onClick(DialogInterface dialog, int which) {?

  ? ? // TODO Auto-generated method stub?

  ? ? Toast.makeText(Main.this, "談不上喜歡不喜歡。", Toast.LENGTH_LONG)?

  ? ? ? .show();?

  ? ? }?

  ? }).create();?

  ? dialog.show();?

3.信息內(nèi)容是一個(gè)簡(jiǎn)單的View類型

創(chuàng)建dialog方法的代碼如下:

new AlertDialog.Builder(this).setTitle("請(qǐng)輸入").setIcon(?

  ? ? android.R.drawable.ic_dialog_info).setView(?

  ? ? new EditText(this)).setPositiveButton("確定", null)?

  ? ? .setNegativeButton("取消", null).show();?

4.信息內(nèi)容是一組單選框

創(chuàng)建dialog方法的代碼如下:

new AlertDialog.Builder(this).setTitle("復(fù)選框").setMultiChoiceItems(?

  ? ? new String[] { "Item1", "Item2" }, null, null)?

  ? ? .setPositiveButton("確定", null)?

  ? ? .setNegativeButton("取消", null).show();?

5.信息內(nèi)容是一組多選框

創(chuàng)建dialog方法的代碼如下:

new AlertDialog.Builder(this).setTitle("單選框").setIcon(?

  ? ? android.R.drawable.ic_dialog_info).setSingleChoiceItems(?

  ? ? new String[] { "Item1", "Item2" }, 0,?

  ? ? new DialogInterface.OnClickListener() {?

  ? ? ? public void onClick(DialogInterface dialog, int which) {?

  ? ? ? dialog.dismiss();?

  ? ? ? }?

  ? ? }).setNegativeButton("取消", null).show();?

6.信息內(nèi)容是一組簡(jiǎn)單列表項(xiàng)

創(chuàng)建dialog的方法代碼如下:

new AlertDialog.Builder(this).setTitle("列表框").setItems(?

  ? ? new String[] { "Item1", "Item2" }, null).setNegativeButton(?

  ? ? "確定", null).show();?

7.信息內(nèi)容是一個(gè)自定義的布局

dialog布局文件代碼如下:

<?xml version="1.0" encoding="utf-8"?>?


  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"?

   android:layout_height="wrap_content" android:layout_width="wrap_content"?

   android:background="#ffffffff" android:orientation="horizontal"?

   android:id="@+id/dialog">?

   <TextView android:layout_height="wrap_content"?

  ? android:layout_width="wrap_content"?

  ? android:id="@+id/tvname" android:text="姓名:" />?

   <EditText android:layout_height="wrap_content"?

  ? android:layout_width="wrap_content" android:id="@+id/etname" android:minWidth="100dip"/>?

  </LinearLayout>?

創(chuàng)建dialog方法的代碼如下:

LayoutInflater inflater = getLayoutInflater();?

  ? View layout = inflater.inflate(R.layout.dialog,?

  ? ? (ViewGroup) findViewById(R.id.dialog));?

  ? new AlertDialog.Builder(this).setTitle("自定義布局").setView(layout)?

  ? ? .setPositiveButton("確定", null)?

  ? ? .setNegativeButton("取消", null).show();?

參考:http://blog.csdn.net/qq457163027/article/details/51192531

http://www.android100.org/html/201302/18/1611.html

二,小的知識(shí)點(diǎn)

1,create().show()和show()都可以是顯示彈出框,兩者的區(qū)別和聯(lián)系:

沒加create()時(shí)show()方法是AlertDialog.Builder類的show()方法;

加了create()方法后的show()方法是Dialog類的show()方法;

而AlertDialog.Builder類的show()方法如下:

AlertDialog.Builder.show()

{

AlertDialog dialog = create();

dialog.show();

return dialog;

}

AlertDialog.Builder類的show()方法里頭自己有create()方法。

2,dialog.dismiss()方法的作用:

讓dialog消失。

在builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterface dialog, int arg1) {

//有沒有這行代碼點(diǎn)擊后彈出框都可以消失。

// dialog.dismiss();

}

});

三, Android自定義Dialog簡(jiǎn)單實(shí)例

做Android應(yīng)用中,最缺少不了的就是自定義Dialog,對(duì)于系統(tǒng)默認(rèn)提供的Dialog樣式,一般都不復(fù)合我們應(yīng)用的樣式。

自定義Dialog需要3步驟即可:

1)、主要的重寫Dialog的Java類

2)、自定義布局文件、并設(shè)置Dialog Theme,在style.xml文件中加一個(gè)即可

3)、使用方法

1、創(chuàng)建CustomPopDialog2.java類

import android.app.Dialog;

import android.content.Context;

import android.graphics.Bitmap;

import android.view.LayoutInflater;

import android.view.View;

import android.view.WindowManager.LayoutParams;

import android.widget.ImageView;

/**

* 該自定義Dialog應(yīng)用在:彈出框居中顯示圖片,點(diǎn)擊其他區(qū)域自動(dòng)關(guān)閉Dialog

*/

public class CustomPopDialog2 extends Dialog {

? ? public CustomPopDialog2(Context context) {

? ? ? ? super(context);

? ? }

? ? public CustomPopDialog2(Context context, int theme) {

? ? ? ? super(context, theme);

? ? }

? ? public static class Builder {

? ? ? ? private Context context;

? ? ? ? private Bitmap image;

? ? ? ? public Builder(Context context) {

? ? ? ? ? ? this.context = context;

? ? ? ? }

? ? ? ? public Bitmap getImage() {

? ? ? ? ? ? return image;

? ? ? ? }

? ? ? ? public void setImage(Bitmap image) {

? ? ? ? ? ? this.image = image;

? ? ? ? }

? ? ? ? public CustomPopDialog2 create() {

? ? ? ? ? ? LayoutInflater inflater = (LayoutInflater) context

? ? ? ? ? ? ? ? ? ? .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

? ? ? ? ? ? final CustomPopDialog2 dialog = new CustomPopDialog2(context,R.style.Dialog);

? ? ? ? ? ? View layout = inflater.inflate(R.layout.dialog_share_qrcode, null);

? ? ? ? ? ? dialog.addContentView(layout, new LayoutParams(

? ? ? ? ? ? ? ? ? ? android.view.ViewGroup.LayoutParams.WRAP_CONTENT

? ? ? ? ? ? ? ? ? ? , android.view.ViewGroup.LayoutParams.WRAP_CONTENT));

? ? ? ? ? ? dialog.setContentView(layout);

? ? ? ? ? ? ImageView img = (ImageView)layout.findViewById(R.id.img_qrcode);

? ? ? ? ? ? img.setImageBitmap(getImage());

? ? ? ? ? ? return dialog;

? ? ? ? }

? ? }

}

這里簡(jiǎn)單說明下,我們自定義Dialog需要準(zhǔn)備一個(gè)自己的View布局文件,主要關(guān)注create()方法即可,本例中就是直接顯示一個(gè)圖片。

2、自定義View的布局文件、并在style.xml中添加theme

<?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:orientation="vertical" android:gravity="center"

? ? android:id="@+id/rootLayout">

? ? <ImageView

? ? ? ? android:id="@+id/img_qrcode"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content"

? ? ? ? android:contentDescription="二維碼" />

</LinearLayout>

樣式:

? ? <style name="Dialog" parent="android:style/Theme.Dialog">

? ? ? ? <item name="android:background">#00000000</item>

? ? ? ? <item name="android:windowBackground">@android:color/transparent</item>

? ? ? ? <item name="android:windowNoTitle">true</item>

? ? ? ? <item name="android:windowIsFloating">true</item>

? ? </style>

3、使用自定義的Dialog

? ? ? ? Bitmap bitmap = xxxxx;// 這里是獲取圖片Bitmap,也可以傳入其他參數(shù)到Dialog中

? ? ? ? CustomPopDialog2.Builder dialogBuild = new CustomPopDialog2.Builder(context);

? ? ? ? dialogBuild.setImage(bitmap);

? ? ? ? CustomPopDialog2 dialog = dialogBuild.create();

? ? ? ? dialog.setCanceledOnTouchOutside(true);// 點(diǎn)擊外部區(qū)域關(guān)閉

? ? ? ? dialog.show();

參考:http://blog.csdn.net/catoop/article/details/50177147

http://blog.csdn.net/duanyanrui/article/details/8494767

四,Android--Dialog詳細(xì)講解

Dialog,對(duì)話框,一個(gè)對(duì)話框就是一個(gè)小窗口,并不會(huì)填滿整個(gè)屏幕,通常是以模態(tài)顯示,要求用戶必須采取行動(dòng)才能繼續(xù)進(jìn)行剩下的操作。

Android提供了豐富的對(duì)話框支持,它提供了如下4中常用的對(duì)話框:

AlertDialog:警告對(duì)話框,使用最廣泛功能最豐富的一個(gè)對(duì)話框。

ProgressDialog:進(jìn)度條對(duì)話框,只是對(duì)進(jìn)度條進(jìn)行了簡(jiǎn)單的封裝。

DatePickerDialog:日期對(duì)話框。

TimePickerDialog:時(shí)間對(duì)話框。

所有的對(duì)話框,都是直接或間接繼承自Dialog類,而AlterDialog直接繼承自Dialog,其他的幾個(gè)類均繼承自AlterDialog。

參考:http://www.kwstu.com/ArticleView/kwstu_20139682354515

http://blog.csdn.net/liang5630/article/details/44098899

五,設(shè)置Dialog使返回鍵和點(diǎn)擊外圍失效(默認(rèn)返回鍵和點(diǎn)擊外圍都可以使其消失)

1,對(duì)于AlertDialog.Builder(this)這種形式創(chuàng)建的Dialog

對(duì)于AlertDialog.Builder(this)這種形式創(chuàng)建的Dialog,只要new AlertDialog.Builder(this).setCancelable(false)這一行就可以使點(diǎn)擊外圍和返回鍵都失效。

2,對(duì)于AlertDialog dlg = new AlertDialog.Builder(this).create();先獲取具體的Dialog的處理

dialog.setCancelable(false);這行代碼也可以使返回鍵和點(diǎn)擊外圍失效。

Dialog的出現(xiàn),很好的提升生了用戶的用戶體驗(yàn),但是對(duì)于咱們程序猿來說,怎么樣來好好的控制用戶的誤操作是個(gè)頭疼問題啊,Dialog默認(rèn)在按下返回鍵的時(shí)候會(huì)消失掉,那么如何讓Dialog在用戶按下返回鍵也不消失呢,方法是有的,畢竟Android是去過太空的,方法就是應(yīng)該截取dialog的key響應(yīng)事件,當(dāng)dialog在前臺(tái)顯示的時(shí)候,keylistener首先會(huì)派發(fā)到dialog里面,在那里面監(jiān)聽就好了。

首先申請(qǐng)一個(gè)keylistener,在里面監(jiān)聽系統(tǒng)的按鍵,當(dāng)然同樣可以監(jiān)聽home鍵和其他的按鍵:

private OnKeyListener keylistener = new DialogInterface.OnKeyListener() {?

? ? ? ? public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {?

? ? ? ? ? ? if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {?

? ? ? ? ? ? ? ? return true;?

? ? ? ? ? ? } else {?

? ? ? ? ? ? ? ? return false;?

? ? ? ? ? ? }?

? ? ? ? }?

? ? };?

然后在你創(chuàng)建Dialog的時(shí)候,將這個(gè)監(jiān)聽注冊(cè)進(jìn)去就哦了,方法如下:

AlertDialog dlg = new AlertDialog.Builder(this).create();?

dlg.setOnKeyListener(keylistener);?

dlg.setCancelable(false);?

dlg.show();?

其他的操作這里就不再累述,這樣就算你按下返回,Dialog就不會(huì)消失了,PS: setCancelable(false),作用是當(dāng)dialog彈出來的時(shí)候,如果觸點(diǎn)在dialog外圍,按照默認(rèn)的方式 dialog將消失。如果這個(gè)設(shè)為false的話 這種情況dialog就不會(huì)消失了。? 加了這一句就OK了 dialog.setCancelable(false);

參考:http://blog.csdn.net/LuckChouDog/article/details/42234801

六,Android界面之----自定義的Dialog,然后利用回調(diào)方法,在調(diào)用處,進(jìn)行Dialog中各按鈕的事件處理

public class LeaveMeetingDialog extends Dialog? implements OnClickListener{

? ? private Button? quitBtn,stopBtn,cancelBtn;

? ? private LeaveMeetingDialogListener listener;

? ? public interface LeaveMeetingDialogListener{

? ? ? ? public void onClick(View view);

? ? }

? ? public LeaveMeetingDialog(Context context,int theme,LeaveMeetingDialogListener listener) {

? ? ? ? super(context,theme);

? ? }

? ? @Override

? ? protected void onCreate(Bundle savedInstanceState) {

? ? ? ? super.onCreate(savedInstanceState);

? ? ? ? setContentView(R.layout.leave_meeting_dialog);

? ? ? ? initViews();

? ? }

? ? private void initViews(){

? ? ? ? quitBtn = (Button)findViewById(R.id.quit_btn);

? ? ? ? stopBtn = (Button)findViewById(R.id.stop_btn);

? ? ? ? cancelBtn = (Button)findViewById(R.id.cancel_btn);

? ? ? ? quitBtn.setOnClickListener(this);

? ? ? ? stopBtn.setOnClickListener(this);

? ? ? ? cancelBtn.setOnClickListener(this);

? ? }

? ? @Override

? ? public void onClick(View v) {

? ? ? ? listener.onClick(v);

? ? }

}

---調(diào)用處:

[java]

LeaveMeetingDialog? dialog = new LeaveMeetingDialog(this,R.style.Theme_CustomDialog,

? ? ? ? ? ? new LeaveMeetingDialogListener() {

? ? ? ? @Override

? ? ? ? public void onClick(View view) {

? ? ? ? ? ? switch(view.getId()){

? ? ? ? ? ? ? ? case R.id.quit_btn:break;

? ? ? ? ? ? ? ? case R.id.stop_btn:break;

? ? ? ? ? ? ? ? case R.id.cancel_btn:break;

? ? ? ? ? ? }

? ? ? ? }

});

? ? ? dialog.show();

參考:http://www.2cto.com/kf/201205/133945.html

七,顯示彈出框后,后面界面不變暗

第一種是在樣式文件styles.xml中添加新的樣式,父樣式指向的是默認(rèn)的Dialog樣式,修改如下,然后你的Dialog用你添加的樣式就可以了.

<resources>?

? ? <style name="DialogStyle" parent="@android:style/Theme.Dialog">?

? ? ? ? <!-- dialog背景樣式 -->

? ? ? ? <item name="android:windowBackground"> @android:color/transparent </item>?

? ? ? ? <!-- 背景透明 -->

? ? ? ? <item name="android:backgroundDimEnabled">false</item>? ? </style>? ? ?

</resources>

第二種是在代碼中修改.lp.alpha大小隨自己要求設(shè)置

// 設(shè)置屏幕背景變暗

private void setScreenBgDarken() {

WindowManager.LayoutParams lp = getWindow().getAttributes();

lp.alpha = 0.5f;

lp.dimAmount = 0.5f;

getWindow().setAttributes(lp);

}

// 設(shè)置屏幕背景變亮

private void setScreenBgLight() {

WindowManager.LayoutParams lp = getWindow().getAttributes();

lp.alpha = 1.0f;

lp.dimAmount = 1.0f;

getWindow().setAttributes(lp);

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 目錄介紹 1.簡(jiǎn)單用法 2.AlertDialog源碼分析2.1 AlertDialog.Builder的構(gòu)造方法...
    楊充211閱讀 1,296評(píng)論 1 1
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程,因...
    小菜c閱讀 7,328評(píng)論 0 17
  • 本人初學(xué)Android,最近做了一個(gè)實(shí)現(xiàn)安卓簡(jiǎn)單音樂播放功能的播放器,收獲不少,于是便記錄下來自己的思路與知識(shí)總結(jié)...
    落日柳風(fēng)閱讀 19,450評(píng)論 2 41
  • 0x000 環(huán)境: 系統(tǒng):Win10 IDE:Android Studio2.0 0x002 簡(jiǎn)單使用 繼承樹:D...
    賣梳子的鯉魚閱讀 532評(píng)論 1 1
  • 今日打卡2:共計(jì)40分鐘 本書完結(jié) 避免錯(cuò)誤推理的陷阱固然重要,但我們還是應(yīng)該花更多精力來領(lǐng)會(huì)邏輯思維的正確原則,...
    吟_f3da閱讀 150評(píng)論 0 0

友情鏈接更多精彩內(nèi)容