最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android中仿IOS提示框的實現(xiàn)方法

 更新時間:2018年01月04日 10:23:02   作者:dreamGong  
下面小編就為大家分享一篇Android中仿IOS提示框的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

前言

在Android開發(fā)中,我們有時需要實現(xiàn)類似IOS的對話框。今天我就來總結(jié)下,如何通過自定義的開發(fā)來實現(xiàn)類似的功能。

自定義Dialog

我們知道Android中最常用的對話框就是Dialog及其派生類。這次我們通過組合的方式來實現(xiàn)一個類似IOS對話框的效果。我們先來看一下布局效果,這個相信大家都能弄出來,在這里我就貼一下最后的效果圖(注意:對話框的邊緣是圓角的)。

效果圖如下:

我們看到,這個和IOS的對話框已經(jīng)非常相似了,后面我們需要做的就是將其作為一個組件封裝起來,實現(xiàn)AlertDialog那樣的調(diào)用方式提供給調(diào)用者使用。下面我們來看一下整個的封裝過程。

1、組合使用Dialog進(jìn)行對象的構(gòu)建

/**
  * 創(chuàng)建BaseDialog實例
  * @return
  */
 public BaseDialog builder(){
  LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  mDialog = new Dialog(mContext, R.style.Dialog);
  //設(shè)置dialog彈出后會點擊屏幕,dialog不消失;點擊物理返回鍵dialog消失
  mDialog.setCanceledOnTouchOutside(false);
  View layout = inflater.inflate(R.layout.dialog_normal_layout, null);
  mDialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
         ViewGroup.LayoutParams.WRAP_CONTENT));
  //設(shè)置Dialog中展示的msg
  mMessage=(TextView) layout.findViewById(R.id.txtMsg);
  //設(shè)置確認(rèn)按鈕的處理事件
  mPositiveBtnText=(TextView) layout.findViewById(R.id.txtSubmit);
  mPositiveBtnText.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    if (mPositiveBtnClickListener != null) {
     mPositiveBtnClickListener.onClick(mDialog, DialogInterface.BUTTON_POSITIVE);
    }
   }
  });
  mNegativeBtnText=(TextView) layout.findViewById(R.id.txtCancle);
  mNegativeBtnText.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    if(mNegativeBtnClickListener!=null) {
     mNegativeBtnClickListener.onClick(mDialog, DialogInterface.BUTTON_NEGATIVE);
    }
   }
  });
  mDialog.setContentView(layout);
  return this;
 }

在這段代碼中,我們看到第7行代碼生成一個Dialog對象,然后通過解析樣式文件(XML)通過setContentView方法添加到生成的Dialog對象中。下面的代碼我們看到通過解析XML樣式文件,來設(shè)置Dialog顯示的文本、取消、確認(rèn)按鈕觸發(fā)的事件等操作。重點在最后一行代碼return this而不是mDialog。因為通過返回this對象我們可以使用“鏈?zhǔn)秸{(diào)用”來不斷的進(jìn)行調(diào)用設(shè)置等操作,類似Android中的AlertDialog使用方式。

2、自定義Dialog的一些設(shè)置

/**
  * 設(shè)置Msg
  * 支持Resource設(shè)置
  * @param message
  */
 public BaseDialog setMessage(int message){
  mMessage.setText(mContext.getText(message).toString());
  return this;
 }
 /**
  * 設(shè)置Msg
  * @param message
  */
 public BaseDialog setMessage(String message){
  mMessage.setText(message);
  return this;
 }

 public BaseDialog setPositiveButton(int positiveBtnText,DialogInterface.OnClickListener listener) {
  mPositiveBtnText.setText(mContext.getText(positiveBtnText));
  mPositiveBtnClickListener = listener;
  return this;
 }
 public BaseDialog setPositiveButton(String positiveBtnText,DialogInterface.OnClickListener listener){
  mPositiveBtnText.setText(positiveBtnText);
  mPositiveBtnClickListener=listener;
  return this;
 }

 public BaseDialog setNegativeButton(int negativeBtnText,DialogInterface.OnClickListener listener){
  mNegativeBtnText.setText(mContext.getText(negativeBtnText));
  mNegativeBtnClickListener=listener;
  return this;
 }

 public BaseDialog setNegativeButton(String negativeBtnText,DialogInterface.OnClickListener listener){
  mNegativeBtnText.setText(negativeBtnText);
  mNegativeBtnClickListener=listener;
  return this;
 }

 public void show(){
  mDialog.show();
 }

我們看到我們提供了幾個比較常用的函數(shù)。特別是setPositiveButton等方法,我們直接將事件處理對象傳遞進(jìn)來了。下面我們來看下自定義控件的字段定義。代碼如下:

public BaseDialog(Context context){
  mContext=context;
 }
 private Dialog mDialog;
 protected Context mContext;
 protected TextView mMessage;
 protected TextView mPositiveBtnText;
 protected TextView mNegativeBtnText;
 protected DialogInterface.OnClickListener mPositiveBtnClickListener;
 protected DialogInterface.OnClickListener mNegativeBtnClickListener;

我們看到整個自定義Dialog的核心就是mDialog對象,通過組合的方式我們在每一個BaseDialog對象內(nèi)部都會有一個Dialog對象,BaseDailog對象只是提供了一些外圍的顯示功能。核心的例如隱藏、顯示、彈出效果等方式依賴于嵌入的mDialog對象的。

下面我們來看一下自定義Dialog的調(diào)用方式。代碼如下:

/**
 * 彈出電話確認(rèn)按鈕
 */
new BaseDialog(getActivity())
  .builder()
  .setMessage("15895991339")
  .setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:15895991339"));
    startActivity(intent);
    dialog.dismiss();
   }
  }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
   @Override
   public void onClick(DialogInterface dialog, int which) {
    dialog.dismiss();
   }
  }).show();

我們看到,調(diào)用方式和AlertDialog的使用方式很相似。

以上這篇Android中仿IOS提示框的實現(xiàn)方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

营山县| 阳城县| 平安县| 宁远县| 汉中市| 碌曲县| 漳州市| 嘉黎县| 三门县| 古丈县| 衡水市| 泗洪县| 浦县| 邹平县| 桐庐县| 定陶县| 咸宁市| 新民市| 怀宁县| 石屏县| 蓝田县| 宁蒗| 新昌县| 威远县| 黔西| 堆龙德庆县| 凤翔县| 三原县| 独山县| 开江县| 金堂县| 徐汇区| 通榆县| 民勤县| 大邑县| 营口市| 合江县| 报价| 都兰县| 元氏县| 偃师市|