Android AlertDialog實(shí)現(xiàn)分享對(duì)話框/退出對(duì)話框/下載對(duì)話框
一.摘要
彈窗通常用于提示用戶進(jìn)行某種操作,比如:點(diǎn)擊分享按鈕,彈窗分享對(duì)話框;雙擊返回按鈕,彈窗退出對(duì)話框;下載文件,提示下載對(duì)話框等等,分享對(duì)話框/退出對(duì)話框/下載對(duì)話框,都可以直接使用AlertDialog實(shí)現(xiàn),類似的效果如下圖:
二.AlertDialog基礎(chǔ)知識(shí)
AlertDialog無(wú)法直接通過new關(guān)鍵字獲取對(duì)象,調(diào)用方法:new AlertDialog.Builder.create()獲取AlertDialog對(duì)象,這個(gè)時(shí)候容易讓人疑惑的是:如何設(shè)置對(duì)話框的屬性?比如:對(duì)話框標(biāo)題,對(duì)話框消息,對(duì)話框按鈕等等
設(shè)置對(duì)話框?qū)傩缘膬煞N方式
第一種:設(shè)置AlertDialog對(duì)象屬性,具體代碼如下:
private void showDialog() { AlertDialog mDialog = null; mDialog = new AlertDialog.Builder(this).create();; mDialog.setIcon(R.drawable.ic_launcher); mDialog.setTitle("系統(tǒng)提示"); mDialog.setMessage("你確定要退出嗎?"); mDialog.setButton(DialogInterface.BUTTON_POSITIVE,"確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finishMyself(); } }); mDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "再按一次退出程序", (int) touchTime) .show(); } }); mDialog.show(); }
第二種:設(shè)置Builder對(duì)象屬性,具體代碼如下:
private void showDialog() { AlertDialog mDialog = null; Builder mBuilder = new AlertDialog.Builder(this); mBuilder.setIcon(R.drawable.ic_launcher); mBuilder.setTitle("系統(tǒng)提示"); mBuilder.setMessage("你確定要退出嗎?"); mBuilder.setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); mBuilder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "再按一次退出程序", (int) touchTime) .show(); } }); mDialog = mBuilder.create();//創(chuàng)建AlertDialog對(duì)象 mDialog.show();//顯示創(chuàng)建的AlertDialog }
這兩種方式的對(duì)話框展示默認(rèn)屬性——對(duì)話框水平垂直居中顯示,對(duì)話框與左右窗體之間有一小段距離,效果圖如下:
如何修改默認(rèn)對(duì)話框?qū)傩裕?/strong>
如何修改AlertDialog對(duì)話框默認(rèn)屬性,然后實(shí)現(xiàn)對(duì)話框內(nèi)容寬度布滿屏幕,高度根據(jù)內(nèi)容自適應(yīng),類似文章開頭點(diǎn)擊分享按鈕,從底部彈出彈窗的效果。首先創(chuàng)建AlertDialog對(duì)話框,然后自定義對(duì)話框的布局View,最后設(shè)置Window對(duì)象屬性。
設(shè)置Window對(duì)象屏幕寬度/高度的三種方式
第一種方式:setLayout()
獲得Window對(duì)象后,設(shè)置Window對(duì)象的布局參數(shù),即調(diào)用setLayout(int width,int height)方法,width取值:android.view.WindowManager.LayoutParams.MATCH_PARENT/android.view.WindowManager.LayoutParams.WRAP_CONTENT,同理height取值:android.view.WindowManager.LayoutParams.MATCH_PARENT/android.view.WindowManager.LayoutParams.WRAP_CONTENT,具體代碼如下:
View view = getLayoutInflater().inflate(R.layout.popup_dialog, null); AlertDialog mDialog = new AlertDialog.Builder(this).create(); mDialog.show();// 顯示創(chuàng)建的AlertDialog,并顯示,必須放在Window設(shè)置屬性之前 /** *設(shè)置mDialog窗口屬性:MATCH_PARENT/WRAP_CONTENT * */ Window window =mDialog.getWindow(); window.setGravity(Gravity.BOTTOM); // 此處可以設(shè)置dialog顯示的位置 window.setLayout(android.view.WindowManager.LayoutParams.MATCH_PARENT, android.view.WindowManager.LayoutParams.WRAP_CONTENT);
第二種方式:setAttributes()
獲得Window對(duì)象后,設(shè)置Window對(duì)象的屬性值,即調(diào)用setAttributes(LayoutParams)方法,LayoutParams的width變量取值:android.view.WindowManager.LayoutParams.MATCH_PARENT/android.view.WindowManager.LayoutParams.WRAP_CONTENT,同理height變量取值:android.view.WindowManager.LayoutParams.MATCH_PARENT/android.view.WindowManager.LayoutParams.WRAP_CONTENT,具體代碼如下:
View view = getLayoutInflater().inflate(R.layout.popup_dialog, null); AlertDialog mDialog = new AlertDialog.Builder(this).create(); mDialog.show();// 顯示創(chuàng)建的AlertDialog,并顯示,必須放在Window設(shè)置屬性之前 Window window =mDialog.getWindow(); window.setGravity(Gravity.BOTTOM); // 此處可以設(shè)置dialog顯示的位置 WindowManager.LayoutParams mParams = window.getAttributes(); mParams.width = android.view.WindowManager.LayoutParams.MATCH_PARENT; mParams.height = android.view.WindowManager.LayoutParams.WRAP_CONTENT; window.setGravity(Gravity.BOTTOM); // 此處可以設(shè)置dialog顯示的位置 window.setAttributes(mParams);
第三種方式:setLayout()
具體代碼如下:
View view = getLayoutInflater().inflate(R.layout.popup_dialog, null); AlertDialog mDialog = new AlertDialog.Builder(this).create(); mDialog.show();// 顯示創(chuàng)建的AlertDialog,并顯示,必須放在Window設(shè)置屬性之前 Window window =mDialog.getWindow(); window.setGravity(Gravity.BOTTOM); // 此處可以設(shè)置dialog顯示的位置 WindowManager manager = getWindowManager(); Display display = manager.getDefaultDisplay(); int width = display.getWidth();//獲取當(dāng)前屏幕寬度 int height = 300;//自定義高度值,比如:300dp window.setGravity(Gravity.BOTTOM); // 此處可以設(shè)置dialog顯示的位置 window.setLayout(width, height);
三.彈窗動(dòng)畫基礎(chǔ)知識(shí)
Android的基本動(dòng)畫包括:漸變動(dòng)畫/平移動(dòng)畫/縮放動(dòng)畫/旋轉(zhuǎn)動(dòng)畫/組合動(dòng)畫,點(diǎn)擊“分享”按鈕,彈窗從底部彈窗,再次點(diǎn)擊彈窗消失,設(shè)置的動(dòng)畫——平移動(dòng)畫,代碼如下:
<?xml version="1.0" encoding="utf-8"?> <!--enter_dialog_anim.xml,彈窗進(jìn)入動(dòng)畫--> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:fromYDelta="100%"> </translate> <?xml version="1.0" encoding="utf-8"?> <!--exit_dialog_anim.xml,彈窗退出動(dòng)畫--> <translate xmlns:android="http://schemas.android.com/apk/res/android" android:duration="300" android:toYDelta="100%" > </translate>
在style.xml文件中添加Window進(jìn)入和退出分別引用的動(dòng)畫類型,代碼如下:
<!-- 分享功能彈窗動(dòng)畫 --> <style name="popup_style" parent="android:Animation"> <item name="@android:windowEnterAnimation">@anim/enter_dialog_anim</item> <item name="@android:windowExitAnimation">@anim/exit_dialog_anim</item> </style>
在Window屬性設(shè)置中調(diào)用setContentView()指定View對(duì)象,同時(shí)調(diào)用setWindowAnimations()指定添加的動(dòng)畫,代碼如下:
window.setContentView(view);//這一步必須指定,否則不出現(xiàn)彈窗
window.setWindowAnimations(R.style.popup_style); // 添加動(dòng)畫
四.自定義彈窗:MyDialogActivity
自定義MyDialogActivity實(shí)現(xiàn)AlertDialog同樣的功能,點(diǎn)擊“分享按鈕”,從窗口底部彈出彈窗,點(diǎn)擊“取消”彈窗消息,最終效果和AlertDialog實(shí)現(xiàn)的彈窗效果一模一樣,如下圖:
開發(fā)步驟:
1.定義布局popup_main.xml。popup_main.xml定義彈窗最終展示的樣子,可以放置多個(gè)平臺(tái)的分享按鈕,比如:微信/微博/空間/人人等,代碼如下:
<?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:background="@color/transparent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/share_weibo_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="@dimen/share_padding" android:layout_weight="1" android:gravity="center_horizontal" android:text="@string/weibo" /> <TextView android:id="@+id/share_weixin_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="@dimen/share_padding" android:layout_weight="1" android:gravity="center_horizontal" android:text="@string/weixin" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:id="@+id/share_kongjian_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="@dimen/share_padding" android:layout_weight="1" android:gravity="center_horizontal" android:text="@string/kongjian" /> <TextView android:id="@+id/share_qq_tv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_margin="@dimen/share_padding" android:layout_weight="1" android:gravity="center_horizontal" android:text="@string/qq" /> </LinearLayout> <Button android:id="@+id/cancel_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="@dimen/activity_vertical_margin" android:background="@drawable/btn_bg" android:text="@string/cancel" /> </LinearLayout>
2.定義Theme樣式。Theme樣式定義在style.xml文件中,在AndroidManifest.xml文件中的標(biāo)簽的android:theme=""屬性中引用,代碼如下:
<!-- MyDialogActivity自定義Threme --> <style name="Theme.CustomDialog" parent="@android:style/Theme.Dialog"> <item name="android:windowNoTitle">true</item> <!-- 設(shè)置title --> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowFrame">@null</item> <!-- 設(shè)置邊框 --> <item name="android:windowIsTranslucent">true</item> <!-- 設(shè)置半透明 --> <item name="android:windowFullscreen">true</item> <!-- 設(shè)置全屏 --> </style> <activity android:name="MyDialogActivity" android:theme="@style/Theme.CustomDialog"/>
3.實(shí)現(xiàn)MyDialogActivity具體功能。
package cn.teachcourse.main; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.TextView; /* @author postmaster@teachcourse.cn @date 創(chuàng)建于:2016-4-14 */ public class MyDialogActivity extends Activity implements OnClickListener { private View view; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); view = getLayoutInflater().inflate(R.layout.popup_main, null, false); setContentView(view); initView(); } private void initView() { Window window = getWindow(); window.setLayout(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT); window.setGravity(Gravity.BOTTOM); window.setWindowAnimations(R.style.popup_style); // 添加動(dòng)畫 TextView weibo_tv = (TextView) view.findViewById(R.id.share_weibo_tv); TextView weixin_tv = (TextView) view.findViewById(R.id.share_weixin_tv); TextView qq_tv = (TextView) view.findViewById(R.id.share_qq_tv); TextView kongjian_tv = (TextView) view .findViewById(R.id.share_kongjian_tv); Button cancel_btn = (Button) view.findViewById(R.id.cancel_btn); // 添加控件事件 weibo_tv.setOnClickListener(this); weixin_tv.setOnClickListener(this); qq_tv.setOnClickListener(this); kongjian_tv.setOnClickListener(this); cancel_btn.setOnClickListener(this); // 調(diào)整圖片的大小/位置 abjustDrawablePos(weibo_tv, R.drawable.share_weibo); abjustDrawablePos(weixin_tv, R.drawable.share_weixin); abjustDrawablePos(kongjian_tv, R.drawable.share_kongjian); abjustDrawablePos(qq_tv, R.drawable.share_qq); } /** * 添加圖標(biāo)和調(diào)整位置 * * @param tv * @param draw */ @SuppressLint("ResourceAsColor") private void abjustDrawablePos(TextView tv, int draw) { Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), draw); mBitmap = centerSquareScaleBitmap(mBitmap, 250); Drawable drawable = new BitmapDrawable(mBitmap); drawable.setBounds(0, 48, 0, 48);// 設(shè)置圖片的邊界 tv.setTextColor(R.color.fontcolor); tv.setCompoundDrawables(null, drawable, null, null);// setCompoundDrawables()和setBounds()方法一起使用 // 添加TextView圖標(biāo) tv.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null); tv.setCompoundDrawablePadding(10);// 設(shè)置圖片和text之間的間距 if (mBitmap != null) { mBitmap = null; drawable = null; } } /** * * @param bitmap * 原圖 * @param edgeLength * 希望得到的正方形部分的邊長(zhǎng) * @return 縮放截取正中部分后的位圖。 */ public static Bitmap centerSquareScaleBitmap(Bitmap bitmap, int edgeLength) { if (null == bitmap || edgeLength <= 0) { return null; } Bitmap result = bitmap; int widthOrg = bitmap.getWidth(); int heightOrg = bitmap.getHeight(); if (widthOrg >= edgeLength && heightOrg >= edgeLength) { // 壓縮到一個(gè)最小長(zhǎng)度是edgeLength的bitmap int longerEdge = (int) (edgeLength * Math.max(widthOrg, heightOrg) / Math .min(widthOrg, heightOrg)); int scaledWidth = widthOrg > heightOrg ? longerEdge : edgeLength; int scaledHeight = widthOrg > heightOrg ? edgeLength : longerEdge; Bitmap scaledBitmap; try { scaledBitmap = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true); } catch (Exception e) { return null; } // 從圖中截取正中間的正方形部分。 int xTopLeft = (scaledWidth - edgeLength) / 2; int yTopLeft = (scaledHeight - edgeLength) / 2; try { result = Bitmap.createBitmap(scaledBitmap, xTopLeft, yTopLeft, edgeLength, edgeLength); scaledBitmap.recycle(); } catch (Exception e) { return null; } } return result; } @Override public void onClick(View v) { switch (v.getId()) { /** * 點(diǎn)擊分享圖標(biāo),彈出分享界面 */ case R.id.share_to_btn: break; case R.id.share_weibo_tv: break; case R.id.share_weixin_tv: break; case R.id.share_qq_tv: break; case R.id.share_kongjian_tv: break; case R.id.cancel_btn: finish(); break; default: break; } } }
4.彈出彈窗,調(diào)用startActivity(this,MyDialogActivity.class)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- Android修改源碼解決Alertdialog觸摸對(duì)話框邊緣消失的問題
- Android對(duì)話框AlertDialog.Builder使用方法詳解
- ANDROID中自定義對(duì)話框AlertDialog使用示例
- Android中AlertDialog 點(diǎn)擊按鈕后不關(guān)閉對(duì)話框的功能
- 簡(jiǎn)析Android多種AlertDialog對(duì)話框效果
- Android使用AlertDialog實(shí)現(xiàn)的信息列表單選、多選對(duì)話框功能
- Android AlertDialog對(duì)話框用法示例
- Android使用AlertDialog創(chuàng)建對(duì)話框
相關(guān)文章
Framework源碼面試之a(chǎn)ctivity啟動(dòng)流程
這篇文章主要為大家介紹了Framework源碼面試之a(chǎn)ctivity啟動(dòng)流程實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09Android 解決嵌套Fragment無(wú)法接收onCreateOptionsMenu事件的問題
本文主要介紹Android Fragment無(wú)法接收onCreateOptionsMenu事件的問題,這里給出解決辦法以及詳細(xì)代碼,希望能幫助有需要的小伙伴2016-07-07Android使用內(nèi)置WebView打開TextView超鏈接的實(shí)現(xiàn)方法
這篇文章主要介紹了Android使用內(nèi)置WebView打開TextView超鏈接的實(shí)現(xiàn)方法,文中給出了詳細(xì)的示例代碼,對(duì)各位Android開發(fā)者們具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03Android自定義照相機(jī)Camera出現(xiàn)黑屏的解決方法
這篇文章主要介紹了Android自定義照相機(jī)Camera出現(xiàn)黑屏的解決方法,分析了黑屏出現(xiàn)的原因及參考解決方法,需要的朋友可以參考下2016-08-08Android播放assets文件里視頻文件相關(guān)問題分析
這篇文章主要介紹了Android播放assets文件里視頻文件相關(guān)問題分析,結(jié)合Android播放assets文件出現(xiàn)錯(cuò)誤的實(shí)際問題給出了原因分析與解決方法參考,需要的朋友可以參考下2016-08-08android基礎(chǔ)總結(jié)篇之八:創(chuàng)建及調(diào)用自己的ContentProvider
這篇文章主要介紹了android基礎(chǔ)總結(jié)篇之八:創(chuàng)建及調(diào)用自己的ContentProvider,有興趣的可以了解一下。2016-11-11