Android對(duì)話框AlertDialog詳解
AlertDialog可以在當(dāng)前的界面上顯示一個(gè)對(duì)話框,這個(gè)對(duì)話框是置頂于所有界面元素之上的,能夠屏蔽掉其他控件的交互能力,因此AlertDialog一般是用于提示一些非常重要的內(nèi)容或者警告信息。
1.創(chuàng)建AlertDialog
首先,我們來了解一下AlertDialog的大體創(chuàng)建順序。與TextView、Button這些控件稍有不同,AlertDialog并不是初始化(findViewById)之后就直接調(diào)用各種方法了。仔細(xì)想想AlertDialog的使用場(chǎng)景, 它并不像TextView和Button那些控件似的一般都是固定在界面上,而是在某個(gè)時(shí)機(jī)才會(huì)觸發(fā)出來(比如用戶點(diǎn)擊了某個(gè)按鈕或者斷網(wǎng)了)。所以AlertDialog并不需要到布局文件中創(chuàng)建,而是在代碼中通過構(gòu)造器(AlertDialog.Builder)來構(gòu)造標(biāo)題、圖標(biāo)和按鈕等內(nèi)容的。
- 1.創(chuàng)建構(gòu)造器AlertDialog.Builder的對(duì)象;
- 2.通過構(gòu)造器對(duì)象調(diào)用setTitle、setMessage、setIcon等方法構(gòu)造對(duì)話框的標(biāo)題、信息和圖標(biāo)等內(nèi)容;
- 3.根據(jù)需要調(diào)用setPositive/Negative/NeutralButton()方法設(shè)置正面按鈕、負(fù)面按鈕和中立按鈕;
- 4.調(diào)用構(gòu)造器對(duì)象的create方法創(chuàng)建AlertDialog對(duì)象;
- 5.AlertDialog對(duì)象調(diào)用show方法,讓對(duì)話框在界面上顯示。
注:AlertDialog.Builder自己也有一個(gè)show方法,可以顯示對(duì)話框,所以上面的第4、第5步可以簡(jiǎn)化為一步。
下面,我們就來創(chuàng)建幾種常用的AlertDialog吧。新建一個(gè)工程,在activity_main.xml布局文件上放置五個(gè)按鈕,點(diǎn)擊按鈕就會(huì)有相應(yīng)的對(duì)話框彈出。
1.1 布局文件代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.fd.alertdialog.MainActivity"> <Button android:id="@+id/btn_normal_dialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="普通對(duì)話框" /> <Button android:id="@+id/btn_item_dialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="普通列表對(duì)話框" /> <Button android:id="@+id/btn_single_choice" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="單選對(duì)話框" /> <Button android:id="@+id/btn_multi_choice" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="復(fù)選對(duì)話框" /> <Button android:id="@+id/btn_custom_dialog" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自定義對(duì)話框" /> </LinearLayout>
1.2 MainActivity的主要代碼如下所示:
package com.fd.alertdialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { public static String TAG = MainActivity.class.getSimpleName(); private int chedkedItem = 0; private String name; private String pwd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bindView(); } private void bindView() { Button btn_normal_dialog = (Button) findViewById(R.id.btn_normal_dialog); Button btn_item_dialog = (Button) findViewById(R.id.btn_item_dialog); Button btn_single_choice = (Button) findViewById(R.id.btn_single_choice); Button btn_multi_choice = (Button) findViewById(R.id.btn_multi_choice); Button btn_custom_dialog = (Button) findViewById(R.id.btn_custom_dialog); btn_normal_dialog.setOnClickListener(this); btn_item_dialog.setOnClickListener(this); btn_single_choice.setOnClickListener(this); btn_multi_choice.setOnClickListener(this); btn_custom_dialog.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_normal_dialog: tipDialog(); //提示對(duì)話框 break; case R.id.btn_item_dialog: itemListDialog(); //列表對(duì)話框 break; case R.id.btn_single_choice: singleChoiceDialog(); //單選對(duì)話框 break; case R.id.btn_multi_choice: multiChoiceDialog(); //多選對(duì)話框 break; case R.id.btn_custom_dialog: customDialog(); //自定義對(duì)話框 break; default: break; } } }
代碼比較簡(jiǎn)單,這里就不做詳細(xì)講解了。接下來看一下各個(gè)對(duì)話框的具體代碼。
2.普通提示對(duì)話框
提示對(duì)話框應(yīng)該是最常見的AlertDialog了,其上主要是提示標(biāo)題,消息主體,底部“取消”、“確定”等按鈕。
/** * 提示對(duì)話框 */ public void tipDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("提示:"); builder.setMessage("這是一個(gè)普通對(duì)話框,"); builder.setIcon(R.mipmap.ic_launcher); builder.setCancelable(true); //點(diǎn)擊對(duì)話框以外的區(qū)域是否讓對(duì)話框消失 //設(shè)置正面按鈕 builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你點(diǎn)擊了確定", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); //設(shè)置反面按鈕 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你點(diǎn)擊了取消", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); //設(shè)置中立按鈕 builder.setNeutralButton("保密", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你選擇了中立", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); AlertDialog dialog = builder.create(); //創(chuàng)建AlertDialog對(duì)象 //對(duì)話框顯示的監(jiān)聽事件 dialog.setOnShowListener(new DialogInterface.OnShowListener() { @Override public void onShow(DialogInterface dialog) { Log.e(TAG, "對(duì)話框顯示了"); } }); //對(duì)話框消失的監(jiān)聽事件 dialog.setOnCancelListener(new DialogInterface.OnCancelListener() { @Override public void onCancel(DialogInterface dialog) { Log.e(TAG, "對(duì)話框消失了"); } }); dialog.show(); //顯示對(duì)話框 }
具體介紹一下用到的方法吧:?
- - setTitle:設(shè)置對(duì)話框的標(biāo)題,比如“提示”、“警告”等;?
- - setMessage:設(shè)置對(duì)話框要傳達(dá)的具體信息;?
- - setIcon: 設(shè)置對(duì)話框的圖標(biāo);?
- - setCancelable: 點(diǎn)擊對(duì)話框以外的區(qū)域是否讓對(duì)話框消失,默認(rèn)為true;?
- - setPositiveButton:設(shè)置正面按鈕,表示“積極”、“確認(rèn)”的意思,第一個(gè)參數(shù)為按鈕上顯示的文字,下同;?
- - setNegativeButton:設(shè)置反面按鈕,表示“消極”、“否認(rèn)”、“取消”的意思;?
- - setNeutralButton:設(shè)置中立按鈕;?
- - setOnShowListener:對(duì)話框顯示時(shí)觸發(fā)的事件;?
- - setOnCancelListener:對(duì)話框消失時(shí)觸發(fā)的事件。
當(dāng)然,這些設(shè)置并不是非要不可,而是根據(jù)自己需要而定。比如標(biāo)題、圖標(biāo)這些就可要可不要。
效果如下圖所示:
你或許會(huì)有這樣的疑問:既然底部那些按鈕的文字和點(diǎn)擊事件的內(nèi)容都是我們自己來寫的,那不是可以把正面按鈕的內(nèi)容和反面按鈕的內(nèi)容互換嗎?看看運(yùn)行后的效果圖就會(huì)發(fā)現(xiàn),反面按鈕是在正面按鈕的左邊的,所以考慮到用戶的操作習(xí)慣和代碼的語義,我們最好還是按照API來寫。
3.普通列表對(duì)話框
列表對(duì)話框的內(nèi)容就是一列顯示內(nèi)容,需要用到構(gòu)造器的
setItems方法,參數(shù)一是列表數(shù)據(jù),參數(shù)二是點(diǎn)擊監(jiān)聽接口,我們要實(shí)現(xiàn)這樣一個(gè)小功能,用戶在點(diǎn)擊某一項(xiàng)時(shí)彈出一個(gè)Toast提示選中項(xiàng)的內(nèi)容。
代碼如下所示:
/** * 列表對(duì)話框 */ private void itemListDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("選擇你喜歡的課程:"); builder.setCancelable(true); final String[] lesson = new String[]{"語文", "數(shù)學(xué)", "英語", "化學(xué)", "生物", "物理", "體育"}; builder.setIcon(R.mipmap.ic_launcher); builder.setIcon(R.mipmap.tab_better_pressed) .setItems(lesson, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "你選擇了" + lesson[which], Toast.LENGTH_SHORT).show(); } }).create(); //設(shè)置正面按鈕 builder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); //設(shè)置反面按鈕 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); AlertDialog dialog = builder.create(); //創(chuàng)建AlertDialog對(duì)象 dialog.show(); //顯示對(duì)話框 }
運(yùn)行后的效果如下所示:
4.單選對(duì)話框
單選對(duì)話框的內(nèi)容就是一個(gè)單項(xiàng)選擇列表,需要用到setSingleChoiceItems
方法,參數(shù)一是列表數(shù)據(jù),參數(shù)二是默認(rèn)選中的item,參數(shù)三則是點(diǎn)擊監(jiān)聽接口,我們要實(shí)現(xiàn)這樣一個(gè)小功能,用戶在選好某一項(xiàng)之后記下其選擇,下次點(diǎn)開對(duì)話框時(shí)就默認(rèn)選中該項(xiàng)。
/** * 單選對(duì)話框 */ public void singleChoiceDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("你現(xiàn)在居住地是:"); final String[] cities = {"北京", "上海", "廣州", "深圳", "杭州", "天津", "成都"}; builder.setSingleChoiceItems(cities, chedkedItem, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(getApplicationContext(), "你選擇了" + cities[which], Toast.LENGTH_SHORT).show(); chedkedItem = which; } }); builder.setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); AlertDialog dialog = builder.create(); //創(chuàng)建AlertDialog對(duì)象 dialog.show(); //顯示對(duì)話框 }
運(yùn)行后的效果如下所示:
你可能會(huì)把checkedItem的賦值放在確定按鈕的點(diǎn)擊事件中,這一看似乎沒什么問題,但是這樣是錯(cuò)誤的!仔細(xì)閱讀谷歌的API文檔就知道了,setSingleChoiceItems 方法中實(shí)現(xiàn)的onClick方法中which表示的是當(dāng)前選中的列表中的item下標(biāo),而setPositiveButton和setNegativeButton方法那里的which表示的卻是按鈕的種類,正面按鈕中的which值是-1,反面按鈕的是-2,與列表的item是沒有關(guān)系的。
例子中的保存選中item的方法有問題的,當(dāng)Activity被銷毀之后重新創(chuàng)建的話數(shù)據(jù)就會(huì)丟失,要想持久化保存的話要用sharedpreferences或者數(shù)據(jù)庫(kù)。
5.復(fù)選對(duì)話框
復(fù)選對(duì)話框是一個(gè)可以重復(fù)選中的列表,與單選對(duì)話框有點(diǎn)像,不過調(diào)用的是setMultiChoiceItems方法,而且多了一個(gè)布爾值參數(shù)isChecked,表示當(dāng)前點(diǎn)擊的item是否被選中。
我們創(chuàng)建一個(gè)集合,將點(diǎn)擊選中的item添加到集合中,取消勾選的話就從集合中移除,點(diǎn)擊確認(rèn)按鈕后就將選中內(nèi)容顯示出來。
/** * 復(fù)選對(duì)話框 */ public void multiChoiceDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("請(qǐng)選擇你喜歡的顏色:"); final String[] colors = {"紅色", "橙色", "黃色", "綠色", "藍(lán)色", "靛色", "紫色"}; final List<String> myColors = new ArrayList<>(); builder.setMultiChoiceItems(colors, null, new DialogInterface.OnMultiChoiceClickListener() { @Override public void onClick(DialogInterface dialog, int which, boolean isChecked) { if (isChecked) { myColors.add(colors[which]); } else { myColors.remove(colors[which]); } } }); builder.setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String result = ""; for (String color : myColors) { result += color + "、"; } Toast.makeText(getApplicationContext(), "你選擇了: " + result, Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { myColors.clear(); dialog.dismiss(); } }); AlertDialog dialog = builder.create(); //創(chuàng)建AlertDialog對(duì)象 dialog.show(); //顯示對(duì)話框 }
運(yùn)行后效果圖如下所示:
6.自定義登錄對(duì)話框
有時(shí)候,只顯示簡(jiǎn)單的標(biāo)題和信息是滿足不了我們的要求,比如我們要實(shí)現(xiàn)一個(gè)登錄對(duì)話框的話,那就需要在對(duì)話框上放置EditText輸入框了。AlertDialog早就為我們準(zhǔn)備好了setView
方法,只要往里面放進(jìn)我們需要的對(duì)話框的View對(duì)象就可以了。
6.1自定義登錄對(duì)話框的布局文件
<?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"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#169ee5" android:gravity="center" android:text="請(qǐng)先登錄" android:textColor="@android:color/white" android:textSize="20sp" /> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請(qǐng)輸入你的賬戶名:" android:textSize="18sp" /> <EditText android:id="@+id/et_pwd" android:inputType="textPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請(qǐng)輸入密碼:" android:textSize="18sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:orientation="horizontal" android:paddingLeft="5dp" android:paddingRight="5dp"> <Button android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:layout_weight="1" android:background="#169ee5" android:text="取消" android:textColor="@android:color/white" android:textSize="16sp" /> <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="#169ee5" android:text="登錄" android:textColor="@android:color/white" android:textSize="16sp" /> </LinearLayout> </LinearLayout>
6.2 自定義對(duì)話框的代碼邏輯
setView
方法是通過AlertDialog的對(duì)象調(diào)用的,所以這里的代碼順序會(huì)稍有不同:我們要先創(chuàng)建AlertDialog對(duì)象和View對(duì)象,然后再去初始化對(duì)話框中的控件。
/** * 自定義登錄對(duì)話框 */ public void customDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); final AlertDialog dialog = builder.create(); View dialogView = View.inflate(MainActivity.this, R.layout.activity_custom, null); dialog.setView(dialogView); dialog.show(); final EditText et_name = dialogView.findViewById(R.id.et_name); final EditText et_pwd = dialogView.findViewById(R.id.et_pwd); final Button btn_login = dialogView.findViewById(R.id.btn_login); final Button btn_cancel = dialogView.findViewById(R.id.btn_cancel); btn_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { name = et_name.getText().toString(); pwd = et_pwd.getText().toString(); if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) { Toast.makeText(MainActivity.this, "用戶名或密碼不能為空!", Toast.LENGTH_SHORT).show(); return; } Toast.makeText(MainActivity.this, "用戶名:" + name + "\n" + "用戶密碼:" + pwd, Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); btn_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); } }); }
運(yùn)行后的效果圖如下所示:
7.自定義對(duì)話框需要注意問題
7.1 系統(tǒng)dialog的寬度
默認(rèn)是固定的,即使你自定義布局怎么修改寬度也不起作用,高度可根據(jù)布局自動(dòng)調(diào)節(jié)。如果想修改彈出窗體大小,可以使用下面這段代碼來實(shí)現(xiàn)改變對(duì)話框的寬高。這段代碼必須在dialog.show()方法之后調(diào)用才有效。
//此處設(shè)置位置窗體大小, dialog.getWindow().setLayout(width,height);
創(chuàng)建新的布局文件activity_layout.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:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#169ee5" android:gravity="center" android:text="請(qǐng)先登錄" android:textColor="@android:color/white" android:textSize="20sp" /> <TextView android:id="@+id/textView4" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#169ee5" android:gravity="center" android:text="請(qǐng)先登錄" android:textColor="@android:color/white" android:textSize="20sp" /> <TextView android:id="@+id/textView3" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#169ee5" android:gravity="center" android:text="請(qǐng)先登錄" android:textColor="@android:color/white" android:textSize="20sp" /> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#169ee5" android:gravity="center" android:text="請(qǐng)先登錄" android:textColor="@android:color/white" android:textSize="20sp" /> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#169ee5" android:gravity="center" android:text="請(qǐng)先登錄" android:textColor="@android:color/white" android:textSize="20sp" /> <EditText android:id="@+id/et_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請(qǐng)輸入你的賬戶名:" android:textSize="18sp" /> <EditText android:id="@+id/et_pwd" android:inputType="textPassword" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="請(qǐng)輸入密碼:" android:textSize="18sp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:orientation="horizontal" android:paddingLeft="5dp" android:paddingRight="5dp"> <Button android:id="@+id/btn_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="10dp" android:layout_weight="1" android:background="#169ee5" android:text="取消" android:textColor="@android:color/white" android:textSize="16sp" /> <Button android:id="@+id/btn_login" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="#169ee5" android:text="登錄" android:textColor="@android:color/white" android:textSize="16sp" /> </LinearLayout> </LinearLayout>
代碼邏輯和6.2的代碼邏輯差不多,只是多了設(shè)置對(duì)話框?qū)挾鹊恼{(diào)用 。
/** * 修改對(duì)話框顯示的寬度 */ public void customDialogDisplay() { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); final AlertDialog dialog = builder.create(); View dialogView = View.inflate(MainActivity.this, R.layout.activity_layout, null); dialog.setView(dialogView); dialog.show(); dialog.getWindow().setLayout(ScreenUtils.getScreenWidth(this)/4*3, LinearLayout.LayoutParams.WRAP_CONTENT); final EditText et_name = dialogView.findViewById(R.id.et_name); final EditText et_pwd = dialogView.findViewById(R.id.et_pwd); final Button btn_login = dialogView.findViewById(R.id.btn_login); final Button btn_cancel = dialogView.findViewById(R.id.btn_cancel); btn_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { name = et_name.getText().toString(); pwd = et_pwd.getText().toString(); if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) { Toast.makeText(MainActivity.this, "用戶名或密碼不能為空!", Toast.LENGTH_SHORT).show(); return; } Toast.makeText(MainActivity.this, "用戶名:" + name + "\n" + "用戶密碼:" + pwd, Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); btn_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); } }); }
ScreenUtils工具類代碼
public class ScreenUtils { /** * 獲取屏幕高度(px) */ public static int getScreenHeight(Context context) { return context.getResources().getDisplayMetrics().heightPixels; } /** * 獲取屏幕寬度(px) */ public static int getScreenWidth(Context context) { return context.getResources().getDisplayMetrics().widthPixels; } }
效果圖:
7.2 改變Android Dialog彈出后的Activity背景亮度:
在代碼中修改.lp.alpha大小,值的大小可根據(jù)自己要求設(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); }
7.3?如何控制彈窗彈出的位置:
一般都是在屏幕正中間彈出默認(rèn),但也可以控制從別的地方彈出,比如從底部彈出,可以這樣寫
private void popFromBottom(Dialog dialog) { Window win = dialog.getWindow(); win.setGravity(Gravity.BOTTOM); // 這里控制彈出的位置 win.getDecorView().setPadding(0, 0, 0, 0); WindowManager.LayoutParams lp = win.getAttributes(); lp.width = WindowManager.LayoutParams.MATCH_PARENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; dialog.getWindow().setBackgroundDrawable(null); win.setAttributes(lp); }
8.代碼下載地址
http://xiazai.jb51.net/202112/yuanma/AlertDialogDemo_jb51.rar
到此這篇關(guān)于Android對(duì)話框AlertDialog詳解的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android 數(shù)據(jù)庫(kù)SQLite 寫入SD卡的方法
如果手機(jī)沒有root,數(shù)據(jù)庫(kù)文件是無法查看到的,不方便調(diào)試。最好的辦法是把數(shù)據(jù)庫(kù)寫進(jìn)SD卡。通過本文給大家介紹Android 數(shù)據(jù)庫(kù)SQLite 寫入SD卡的方法,需要的朋友參考下吧2016-04-04Android編程使用WebView實(shí)現(xiàn)與Javascript交互的方法【相互調(diào)用參數(shù)、傳值】
這篇文章主要介紹了Android編程使用WebView實(shí)現(xiàn)與Javascript交互的方法,可實(shí)現(xiàn)基于WebView與JavaScript相互調(diào)用參數(shù)、傳值的功能,需要的朋友可以參考下2017-03-03Android實(shí)現(xiàn)單頁(yè)面浮層可拖動(dòng)view的一種方法
本篇文章主要介紹了Android實(shí)現(xiàn)單頁(yè)面浮層可拖動(dòng)view的一種方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10Android實(shí)現(xiàn)內(nèi)存中數(shù)據(jù)保存到sdcard的方法
這篇文章主要介紹了Android實(shí)現(xiàn)內(nèi)存中數(shù)據(jù)保存到sdcard的方法,涉及Android的文件讀寫與I/O操作相關(guān)技巧,需要的朋友可以參考下2016-01-01Moshi?完美解決Gson在kotlin中默認(rèn)值空的問題詳解
這篇文章主要為大家介紹了Moshi?完美解決Gson在kotlin中默認(rèn)值空的問題詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03