Android實現(xiàn)類似iOS風(fēng)格的對話框?qū)嵗a
分享一個簡單的常用的對話框類,按照國際慣例,先上圖
布局簡單,先上布局。一個標(biāo)題,一個內(nèi)容,兩個按鈕
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="270dp" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:background="@drawable/shape_diglog_bg" android:orientation="vertical"> <TextView android:id="@+id/dialog_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginTop="10dp" android:text="這里是標(biāo)題" android:textColor="#333333" android:textSize="19sp" android:visibility="visible" /> <TextView android:id="@+id/dialog_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="30dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="30dp" android:text="這里是內(nèi)容" android:textColor="#333333" android:textSize="17sp" android:textStyle="bold" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="18sp" android:background="#f1f1f1" /> <LinearLayout android:id="@+id/ll_button" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <Button android:id="@+id/dialog_cancel" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@null" android:text="取消" android:textColor="#006DFF" android:textSize="17sp" /> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="#f1f1f1" /> <Button android:id="@+id/dialog_ensure" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@null" android:text="確定" android:textColor="#006DFF" android:textSize="17sp" /> </LinearLayout> </LinearLayout> </RelativeLayout>
接著就是自定義類!
首先,新建類繼承Dialog
public class CommonDialog extends Dialog
接著是構(gòu)造函數(shù),在構(gòu)造函數(shù)中定義樣式
public CommonDialog(@NonNull Context context) { super(context, R.style.dialog_Common); mContext = context; setContentView(R.layout.dialog_common); ButterKnife.bind(this); }
其中,在style中定義對話框?qū)傩?/p>
<style name="dialog_Common" parent="android:style/Theme.Dialog"> <!--說明提示框是否有邊框--> <item name="android:windowFrame">@null</item> <!--說明提示框是否有標(biāo)題--> <item name="android:windowNoTitle">true</item> <!--設(shè)置提示框的背景顏色是什么--> <item name="android:windowBackground">@android:color/transparent</item> <!--是否浮現(xiàn)在activity之上--> <item name="android:windowIsFloating">true</item> <!--是否有遮蓋--> <item name="android:windowContentOverlay">@null</item> <!--說明提示框是滯是透明的--> <item name="android:windowIsTranslucent">true</item> <!--說明是否充許對話框的背景變暗。為true則充許變暗--> <item name="android:backgroundDimEnabled">true</item> <!--設(shè)置背景透明度--> <item name="android:backgroundDimAmount">0.4</item> </style>
緊接著,提供四個變量來存儲設(shè)置的標(biāo)題、內(nèi)容以及兩個按鈕的文字
private String title; private String content; private String cancel; private String ensure;
現(xiàn)在需要提供能夠設(shè)置標(biāo)題、內(nèi)容以及兩個按鈕的文字的方法
/* * 設(shè)置標(biāo)題 默認(rèn)沒有標(biāo)題 * * @param title */ public void setTitle(String title) { this.title = title; } /** * 設(shè)置內(nèi)容 默認(rèn)為空 * * @param content */ public void setContent(String content) { this.content = content; } /** * 設(shè)置確定按鈕內(nèi)容 默認(rèn)為確定 * * @param ensure */ public void setEnsure(String ensure) { this.ensure = ensure; } /** * 設(shè)置取消按鈕內(nèi)容 默認(rèn)為取消 * * @param cancel */ public void setCancel(String cancel) { this.cancel = cancel; }
現(xiàn)在,處理按鈕的點擊事件
/** * 確定按鈕事件監(jiān)聽 默認(rèn)是dismiss對話框 * * @param onEnsureClickListener */ public void setOnEnsureClickListener(View.OnClickListener onEnsureClickListener) { this.onEnsureClickListener = onEnsureClickListener; } /** * 取消按鈕事件監(jiān)聽 默認(rèn)是dismiss對話框 * * @param onCabcelClickListener */ public void setOnCancelClickListener(View.OnClickListener onCabcelClickListener) { this.onCancelClickListener = onCabcelClickListener; }
默認(rèn)的是點擊對話框消失
/** * 默認(rèn)點擊事件,點擊彈框消失 */ private View.OnClickListener onClickListenerDismiss = new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); } };
最后,重寫父類的show方法,將展示之前設(shè)置的各種信息
/** * 重寫show方法 */ @Override public void show() { if (TextUtils.isEmpty(title)) { //默認(rèn)沒有標(biāo)題 dialogTitle.setVisibility(View.GONE); } else { //默認(rèn)不設(shè)置內(nèi)容,則內(nèi)容太為空 dialogTitle.setVisibility(View.VISIBLE); setTextViewTxt(dialogTitle, title); } if (TextUtils.isEmpty(cancel)) { //默認(rèn)取消按鈕文字為"取消" cancel = mContext.getString(R.string.cancel); } if (TextUtils.isEmpty(ensure)) { //默認(rèn)確認(rèn)按鈕文字為"確認(rèn)" ensure = mContext.getString(R.string.ensure); } //設(shè)置文字信息 setTextViewTxt(dialogContent, content); setTextViewTxt(dialogCancel, cancel); setTextViewTxt(dialogEnsure, ensure); //設(shè)置點擊事件 setButtonOnClickListener(dialogCancel, onCancelClickListener); setButtonOnClickListener(dialogEnsure, onEnsureClickListener); super.show(); }
最最后,獻(xiàn)上在Activity中如何使用該對話框的方法的代碼
public class MainActivity extends AppCompatActivity { @BindView(R.id.btn_demo_haveTitle) Button btnDemoHaveTitle; @BindView(R.id.btn_demo_noTitle) Button btnDemoNoTitle; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } @OnClick({R.id.btn_demo_haveTitle, R.id.btn_demo_noTitle}) public void onClick(View view) { switch (view.getId()) { //沒有標(biāo)題的對話框 case R.id.btn_demo_haveTitle: final CommonDialog dialog1 = new CommonDialog(this); dialog1.setTitle("提示"); dialog1.setContent("是否確認(rèn)退出?"); dialog1.setOnEnsureClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,"點擊了確認(rèn)",Toast.LENGTH_SHORT).show(); // TODO: 2017/9/17 這里寫你的代碼 dialog1.dismiss(); } }); dialog1.show(); break; //有標(biāo)題的對話框 case R.id.btn_demo_noTitle: final CommonDialog dialog2 = new CommonDialog(this); //不設(shè)置標(biāo)題默認(rèn)沒有標(biāo)題 dialog2.setContent("是否確認(rèn)退出?"); dialog2.setOnEnsureClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,"點擊了確認(rèn)",Toast.LENGTH_SHORT).show(); // TODO: 2017/9/17 這里寫你的代碼 dialog2.dismiss(); } }); dialog2.show(); break; } } }
總結(jié)
以上所述是小編給大家介紹的Android實現(xiàn)類似iOS風(fēng)格的對話框樣式,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- 詳解Android中提示對話框(ProgressDialog和DatePickerDialog和TimePickerDialog&PopupWindow)
- Android自定義PopupWindow仿點擊彈出分享功能
- android自定義popupwindow仿微信右上角彈出菜單效果
- Android自定義PopupWindow簡單小例子
- Android自定義彈出窗口PopupWindow使用技巧
- Android中自定義PopupWindow實現(xiàn)彈出框并帶有動畫效果
- Android仿IOS底部彈出對話框
- android底部彈出iOS7風(fēng)格對話選項框(QQ對話框)--第三方開源之IOS_Dialog_Library
- Android自定義PopupWindow實現(xiàn)炫酷的IOS對話框效果
相關(guān)文章
詳談Android中Matrix的set、pre、post的區(qū)別
下面小編就為大家?guī)硪黄斦凙ndroid中Matrix的set、pre、post的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04Android中ViewFlipper和AdapterViewFlipper使用的方法實例
ViewFlipper和AdapterViewFlipper是Android自帶的一個多頁面管理控件,下面這篇文章主要給大家介紹了關(guān)于Android中ViewFlipper和AdapterViewFlipper使用的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05Android ListView實現(xiàn)仿iPhone實現(xiàn)左滑刪除按鈕的簡單實例
下面小編就為大家?guī)硪黄狝ndroid ListView實現(xiàn)仿iPhone實現(xiàn)左滑刪除按鈕的簡單實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08使用Flutter 構(gòu)建Web應(yīng)用邏輯解析
這篇文章主要為大家介紹了使用Flutter 構(gòu)建Web應(yīng)用邏輯解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12從零開始學(xué)android實現(xiàn)計算器功能示例分享(計算器源碼)
這篇文章主要介紹了android實現(xiàn)的計算器功能示例,可以加減乘除;可以倒退,可以清空文本,大家參考使用吧2014-02-02