Android 自定義一套 Dialog通用提示框 (代碼庫)
做Android開發(fā)五年了,期間做做停停(去做后臺開發(fā),服務(wù)器管理),當回來做Android的時候,發(fā)現(xiàn)很生疏,好些控件以前寫得很順手,現(xiàn)在好像忘記些什么了,總要打開這個項目,打開那個項目,有時未必還找得到。
總結(jié)起來,還是源于沒有好好做一個屬于自己的代碼庫,把平時開發(fā)項目中一些自定義的控件,或一些耦合性很低的模塊封裝起來,或者平時比較少寫博客。如果你是一個剛學會開發(fā)的程序猿,或者是有過好幾年開發(fā)經(jīng)驗的大鳥,也該開始整理整理自己的代碼,這也不枉此生敲代碼的歲月,同時在面試中,也會給你帶來不少印象分喔。所以,我也開始準備自己的代碼庫,放在github 或者微博上,希望可以跟各位大神多交流。下面我先放一到兩個自定義控件。
自定義一套 Dialog通用提示框:
上訴提示框都是一種類型,當然有可能你不大滿意,或者與你們設(shè)計師的要求的風格不一致,沒關(guān)系,你只要進去修改一下dialog 的布局就可以了。當然,我希望在自定義這些控件的時候,能用xml 來渲染的,盡量不要用圖片去做背景之類的。每個app 的提示框風格其實大體一致的,不會每個頁面的提示框都不一樣,如果真的變化太大,我們就重新自定義一個dialog即可。其它的只需設(shè)置一下信息即可:
new CommomDialog(mContext, R.style.dialog, "您確定刪除此信息?", new CommomDialog.OnCloseListener() { @Override public void onClick(boolean confirm) { if(confirm){ Toast.makeText(this,"點擊確定", Toast.LENGTH_SHORT).show(); dialog.dismiss(); } } }) .setTitle("提示").show();
我們先看 CommomDialog 類, 這個類定義的時候,里面的方法盡量做成鏈式的,方便后期調(diào)用
public class CommomDialog extends Dialog implements View.OnClickListener{ private TextView contentTxt; private TextView titleTxt; private TextView submitTxt; private TextView cancelTxt; private Context mContext; private String content; private OnCloseListener listener; private String positiveName; private String negativeName; private String title; public CommomDialog(Context context) { super(context); this.mContext = context; } public CommomDialog(Context context, int themeResId, String content) { super(context, themeResId); this.mContext = context; this.content = content; } public CommomDialog(Context context, int themeResId, String content, OnCloseListener listener) { super(context, themeResId); this.mContext = context; this.content = content; this.listener = listener; } protected CommomDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); this.mContext = context; } public CommomDialog setTitle(String title){ this.title = title; return this; } public CommomDialog setPositiveButton(String name){ this.positiveName = name; return this; } public CommomDialog setNegativeButton(String name){ this.negativeName = name; return this; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.dialog_commom); setCanceledOnTouchOutside(false); initView(); } private void initView(){ contentTxt = (TextView)findViewById(R.id.content); titleTxt = (TextView)findViewById(R.id.title); submitTxt = (TextView)findViewById(R.id.submit); submitTxt.setOnClickListener(this); cancelTxt = (TextView)findViewById(R.id.cancel); cancelTxt.setOnClickListener(this); contentTxt.setText(content); if(!TextUtils.isEmpty(positiveName)){ submitTxt.setText(positiveName); } if(!TextUtils.isEmpty(negativeName)){ cancelTxt.setText(negativeName); } if(!TextUtils.isEmpty(title)){ titleTxt.setText(title); } } @Override public void onClick(View v) { switch (v.getId()){ case R.id.cancel: if(listener != null){ listener.onClick(this, false); } this.dismiss(); break; case R.id.submit: if(listener != null){ listener.onClick(this, true); } break; } } public interface OnCloseListener{ void onClick(Dialog dialog, boolean confirm); } }
自定義了監(jiān)聽事件,設(shè)置了消息后,返回該句柄, return this;
再看看 R.layout.dialog_commom 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:background="@drawable/bg_round_white" android:orientation="vertical" > <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:padding="12dp" android:layout_marginTop="12dp" android:text="提示" android:textSize="16sp" android:textColor="@color/black"/> <TextView android:id="@+id/content" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_gravity="center_horizontal" android:lineSpacingExtra="3dp" android:layout_marginLeft="40dp" android:layout_marginTop="20dp" android:layout_marginRight="40dp" android:layout_marginBottom="30dp" android:text="簽到成功,獲得200積分" android:textSize="12sp" android:textColor="@color/font_common_1"/> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/commom_background"/> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal"> <TextView android:id="@+id/cancel" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg_dialog_left_white" android:layout_weight="1.0" android:gravity="center" android:text="@string/cancel" android:textSize="12sp" android:textColor="@color/font_common_2"/> <View android:layout_width="1dp" android:layout_height="match_parent" android:background="@color/commom_background"/> <TextView android:id="@+id/submit" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg_dialog_right_white" android:gravity="center" android:layout_weight="1.0" android:text="@string/submit" android:textSize="12sp" android:textColor="@color/font_blue"/> </LinearLayout> </LinearLayout>
整個背景我使用了圓角,這樣不顯得特別生硬 android:background="@drawable/bg_round_white"
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/white" /> <corners android:radius="8dp" /> </shape>
當然底部兩個按鈕也是要做相應(yīng)的圓角處理:
左下按鈕:android:background="@drawable/bg_dialog_left_white"
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/white" /> <corners android:bottomLeftRadius="8dp" /> </shape>
右下按鈕:android:background="@drawable/bg_dialog_right_white"
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@color/white" /> <corners android:bottomRightRadius="8dp" /> </shape>
展示的 style 也要設(shè)置一下:
<style name="dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <!--邊框--> <item name="android:windowIsFloating">true</item> <!--是否浮現(xiàn)在activity之上--> <item name="android:windowIsTranslucent">false</item> <!--半透明--> <item name="android:windowNoTitle">true</item> <!--無標題--> <item name="android:windowBackground">@android:color/transparent</item> <!--背景透明--> <item name="android:backgroundDimEnabled">true</item> <!--模糊--> </style>
這樣基本大功告成,通過設(shè)置消息頭,信息體,按鈕名稱,還有點擊事件,就可以隨意控制你的提示框了。
源碼鏈接:https://github.com/xiaoxiaoqingyi/mine-android-repository
后面我會把自己的代碼庫都放上來,與大家一起學習。
以上所述是小編給大家介紹的Android 自定義一套 Dialog通用提示框 (代碼庫),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
- Android實現(xiàn)簡單的popupwindow提示框
- Android中仿IOS提示框的實現(xiàn)方法
- Android使用Toast顯示消息提示框
- IOS 仿Android吐司提示框的實例(分享)
- Android仿IOS自定義AlertDialog提示框
- Android仿QQ、微信聊天界面長按提示框效果
- Android仿百度谷歌搜索自動提示框AutoCompleteTextView簡單應(yīng)用示例
- Android超實用的Toast提示框優(yōu)化分享
- Android實現(xiàn)Toast提示框圖文并存的方法
- Android編程之自定義AlertDialog(退出提示框)用法實例
- Android模擬美團客戶端進度提示框
- android 彈出提示框的使用(圖文實例)
- android實現(xiàn)彈出提示框
相關(guān)文章
Android 使用Glide加載網(wǎng)絡(luò)圖片等比例縮放的實現(xiàn)方法
這篇文章主要介紹了Android 使用Glide加載網(wǎng)絡(luò)圖片等比例縮放的實現(xiàn)方法,需要的朋友可以參考下2018-08-08Android中設(shè)置RadioButton在文字右邊的方法實例
這篇文章主要介紹了Android中設(shè)置RadioButton在文字右邊的方法實例,本文直接給出XML配置實現(xiàn)代碼,需要的朋友可以參考下2015-04-04android開發(fā)之橫向滾動/豎向滾動的ListView(固定列頭)
由于項目需要,我們需要一個可以橫向滾動的,又可以豎向滾動的 表格;經(jīng)過幾天的研究終于搞定,感興趣的朋友可以了解下哦2013-01-01Android開發(fā)實現(xiàn)根據(jù)包名判斷App運行狀態(tài)的方法
這篇文章主要介紹了Android開發(fā)實現(xiàn)根據(jù)包名判斷App運行狀態(tài)的方法,結(jié)合實例形式分析了Android結(jié)合包名判斷app運行狀態(tài)的方法,需要的朋友可以參考下2017-11-11