国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 自定義一套 Dialog通用提示框 (代碼庫)

2019-10-23 18:30:50
字體:
供稿:網(wǎng)友

   做Android開發(fā)五年了,期間做做停停(去做后臺開發(fā),服務(wù)器管理),當(dāng)回來做Android的時候,發(fā)現(xiàn)很生疏,好些控件以前寫得很順手,現(xiàn)在好像忘記些什么了,總要打開這個項(xiàng)目,打開那個項(xiàng)目,有時未必還找得到。

      總結(jié)起來,還是源于沒有好好做一個屬于自己的代碼庫,把平時開發(fā)項(xiàng)目中一些自定義的控件,或一些耦合性很低的模塊封裝起來,或者平時比較少寫博客。如果你是一個剛學(xué)會開發(fā)的程序猿,或者是有過好幾年開發(fā)經(jīng)驗(yàn)的大鳥,也該開始整理整理自己的代碼,這也不枉此生敲代碼的歲月,同時在面試中,也會給你帶來不少印象分喔。所以,我也開始準(zhǔn)備自己的代碼庫,放在github 或者微博上,希望可以跟各位大神多交流。下面我先放一到兩個自定義控件。 

自定義一套 Dialog通用提示框:

     上訴提示框都是一種類型,當(dāng)然有可能你不大滿意,或者與你們設(shè)計(jì)師的要求的風(fēng)格不一致,沒關(guān)系,你只要進(jìn)去修改一下dialog 的布局就可以了。當(dāng)然,我希望在自定義這些控件的時候,能用xml 來渲染的,盡量不要用圖片去做背景之類的。每個app 的提示框風(fēng)格其實(shí)大體一致的,不會每個頁面的提示框都不一樣,如果真的變化太大,我們就重新自定義一個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 類, 這個類定義的時候,里面的方法盡量做成鏈?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>

整個背景我使用了圓角,這樣不顯得特別生硬 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)然底部兩個按鈕也是要做相應(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

后面我會把自己的代碼庫都放上來,與大家一起學(xué)習(xí)。

以上所述是小編給大家介紹的Android 自定義一套 Dialog通用提示框 (代碼庫),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對VEVB武林網(wǎng)網(wǎng)站的支持!

 

注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 青海省| 石景山区| 巫溪县| 涞水县| 鹿邑县| 临澧县| 泰宁县| 武威市| 罗定市| 阿克陶县| 朝阳区| 牡丹江市| 巴中市| 息烽县| 邻水| 通州市| 鄂尔多斯市| 昭通市| 龙井市| 柳河县| 堆龙德庆县| 宜川县| 金门县| 若羌县| 蓬莱市| 民丰县| 岳阳市| 双柏县| 深泽县| 和政县| 霍城县| 十堰市| 五家渠市| 基隆市| 永仁县| 如东县| 舒城县| 河间市| 喜德县| 永昌县| 洛浦县|