Android 自定義一套 Dialog通用提示框 (代碼庫(kù))
做Android開發(fā)五年了,期間做做停停(去做后臺(tái)開發(fā),服務(wù)器管理),當(dāng)回來做Android的時(shí)候,發(fā)現(xiàn)很生疏,好些控件以前寫得很順手,現(xiàn)在好像忘記些什么了,總要打開這個(gè)項(xiàng)目,打開那個(gè)項(xiàng)目,有時(shí)未必還找得到。
總結(jié)起來,還是源于沒有好好做一個(gè)屬于自己的代碼庫(kù),把平時(shí)開發(fā)項(xiàng)目中一些自定義的控件,或一些耦合性很低的模塊封裝起來,或者平時(shí)比較少寫博客。如果你是一個(gè)剛學(xué)會(huì)開發(fā)的程序猿,或者是有過好幾年開發(fā)經(jīng)驗(yàn)的大鳥,也該開始整理整理自己的代碼,這也不枉此生敲代碼的歲月,同時(shí)在面試中,也會(huì)給你帶來不少印象分喔。所以,我也開始準(zhǔn)備自己的代碼庫(kù),放在github 或者微博上,希望可以跟各位大神多交流。下面我先放一到兩個(gè)自定義控件。
自定義一套 Dialog通用提示框:
上訴提示框都是一種類型,當(dāng)然有可能你不大滿意,或者與你們?cè)O(shè)計(jì)師的要求的風(fēng)格不一致,沒關(guān)系,你只要進(jìn)去修改一下dialog 的布局就可以了。當(dāng)然,我希望在自定義這些控件的時(shí)候,能用xml 來渲染的,盡量不要用圖片去做背景之類的。每個(gè)app 的提示框風(fēng)格其實(shí)大體一致的,不會(huì)每個(gè)頁(yè)面的提示框都不一樣,如果真的變化太大,我們就重新自定義一個(gè)dialog即可。其它的只需設(shè)置一下信息即可:
new CommomDialog(mContext, R.style.dialog, "您確定刪除此信息?", new CommomDialog.OnCloseListener() {
@Override
public void onClick(boolean confirm) {
if(confirm){
Toast.makeText(this,"點(diǎn)擊確定", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
})
.setTitle("提示").show();
我們先看 CommomDialog 類, 這個(gè)類定義的時(shí)候,里面的方法盡量做成鏈?zhǔn)降?,方便后期調(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>
整個(gè)背景我使用了圓角,這樣不顯得特別生硬 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>
當(dāng)然底部?jī)蓚€(gè)按鈕也是要做相應(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> <!--無標(biāo)題--> <item name="android:windowBackground">@android:color/transparent</item> <!--背景透明--> <item name="android:backgroundDimEnabled">true</item> <!--模糊--> </style>
這樣基本大功告成,通過設(shè)置消息頭,信息體,按鈕名稱,還有點(diǎn)擊事件,就可以隨意控制你的提示框了。
源碼鏈接:https://github.com/xiaoxiaoqingyi/mine-android-repository
后面我會(huì)把自己的代碼庫(kù)都放上來,與大家一起學(xué)習(xí)。
以上所述是小編給大家介紹的Android 自定義一套 Dialog通用提示框 (代碼庫(kù)),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android實(shí)現(xiàn)簡(jiǎn)單的popupwindow提示框
- Android中仿IOS提示框的實(shí)現(xiàn)方法
- Android使用Toast顯示消息提示框
- IOS 仿Android吐司提示框的實(shí)例(分享)
- Android仿IOS自定義AlertDialog提示框
- Android仿QQ、微信聊天界面長(zhǎng)按提示框效果
- Android仿百度谷歌搜索自動(dòng)提示框AutoCompleteTextView簡(jiǎn)單應(yīng)用示例
- Android超實(shí)用的Toast提示框優(yōu)化分享
- Android實(shí)現(xiàn)Toast提示框圖文并存的方法
- Android編程之自定義AlertDialog(退出提示框)用法實(shí)例
- Android模擬美團(tuán)客戶端進(jìn)度提示框
- android 彈出提示框的使用(圖文實(shí)例)
- android實(shí)現(xiàn)彈出提示框
相關(guān)文章
Android 使用Glide加載網(wǎng)絡(luò)圖片等比例縮放的實(shí)現(xiàn)方法
這篇文章主要介紹了Android 使用Glide加載網(wǎng)絡(luò)圖片等比例縮放的實(shí)現(xiàn)方法,需要的朋友可以參考下2018-08-08
Android中設(shè)置RadioButton在文字右邊的方法實(shí)例
這篇文章主要介紹了Android中設(shè)置RadioButton在文字右邊的方法實(shí)例,本文直接給出XML配置實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04
android動(dòng)態(tài)壁紙調(diào)用的簡(jiǎn)單實(shí)例
動(dòng)態(tài)壁紙的實(shí)現(xiàn)其實(shí)就是在Activity中調(diào)用動(dòng)態(tài)壁紙服務(wù),通過綁定服務(wù)得到IWallpaperService,調(diào)用該接口中的attach函數(shù)實(shí)現(xiàn)壁紙的調(diào)用。2013-06-06
android開發(fā)之橫向滾動(dòng)/豎向滾動(dòng)的ListView(固定列頭)
由于項(xiàng)目需要,我們需要一個(gè)可以橫向滾動(dòng)的,又可以豎向滾動(dòng)的 表格;經(jīng)過幾天的研究終于搞定,感興趣的朋友可以了解下哦2013-01-01
Android開發(fā)實(shí)現(xiàn)根據(jù)包名判斷App運(yùn)行狀態(tài)的方法
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)根據(jù)包名判斷App運(yùn)行狀態(tài)的方法,結(jié)合實(shí)例形式分析了Android結(jié)合包名判斷app運(yùn)行狀態(tài)的方法,需要的朋友可以參考下2017-11-11

