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

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

Android自定義Dialog總結(jié)

2019-11-06 09:41:52
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

     定義一個(gè)類然后去繼承Dialog類,然后重寫相應(yīng)的構(gòu)造器方法.大家都知道一般的對(duì)話框的創(chuàng)建過(guò)程都是來(lái)一個(gè)AlertDialog.Builder對(duì)象,然后使用一些set方法來(lái)設(shè)置標(biāo)題內(nèi)容以及設(shè)置一些自定義的view和點(diǎn)擊的Button以及相應(yīng)的點(diǎn)擊事件并且可以采用鏈?zhǔn)骄幊桃贿B串設(shè)置一些屬性.運(yùn)用到了設(shè)計(jì)模式中的建造者模式的方式來(lái)實(shí)現(xiàn)自定義控件的實(shí)現(xiàn)。

public class MapDialog extends Dialog{public MapDialog(Context context) {          super(context);      }        public MapDialog(Context context, int theme) {          super(context, theme);      }          public static class Builder {          PRivate Context context;          private String title;          private String message;          private String positiveButtonText;          private String negativeButtonText;          private View contentView;          private DialogInterface.OnClickListener positiveButtonClickListener;          private DialogInterface.OnClickListener negativeButtonClickListener;            public Builder(Context context) {              this.context = context;          }            public Builder setMessage(String message) {              this.message = message;              return this;          }            /**          * Set the Dialog message from resource          *           * @param title          * @return          */          public Builder setMessage(int message) {              this.message = (String) context.getText(message);              return this;          }            /**          * Set the Dialog title from resource          *           * @param title          * @return          */          public Builder setTitle(int title) {              this.title = (String) context.getText(title);              return this;          }            /**          * Set the Dialog title from String          *           * @param title          * @return          */            public Builder setTitle(String title) {              this.title = title;              return this;          }            public Builder setContentView(View v) {              this.contentView = v;              return this;          }            /**          * Set the positive button resource and it's listener          *           * @param positiveButtonText          * @return          */          public Builder setPositiveButton(int positiveButtonText,                  DialogInterface.OnClickListener listener) {              this.positiveButtonText = (String) context                      .getText(positiveButtonText);              this.positiveButtonClickListener = listener;              return this;          }            public Builder setPositiveButton(String positiveButtonText,                  DialogInterface.OnClickListener listener) {              this.positiveButtonText = positiveButtonText;              this.positiveButtonClickListener = listener;              return this;          }            public Builder setNegativeButton(int negativeButtonText,                  DialogInterface.OnClickListener listener) {              this.negativeButtonText = (String) context                      .getText(negativeButtonText);              this.negativeButtonClickListener = listener;              return this;          }            public Builder setNegativeButton(String negativeButtonText,                  DialogInterface.OnClickListener listener) {              this.negativeButtonText = negativeButtonText;              this.negativeButtonClickListener = listener;              return this;          }            public MapDialog create() {              LayoutInflater inflater = (LayoutInflater) context                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);              // instantiate the dialog with the custom Theme              final MapDialog dialog = new MapDialog(context);              View layout = inflater.inflate(R.layout.dialog_normal_layout, null);              dialog.addContentView(layout, new LayoutParams(                      LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));              // set the dialog title              ((TextView) layout.findViewById(R.id.title)).setText(title);              // set the confirm button              if (positiveButtonText != null) {                  ((Button) layout.findViewById(R.id.positiveButton_ok))                          .setText(positiveButtonText);                  if (positiveButtonClickListener != null) {                      ((Button) layout.findViewById(R.id.negativeButton_cancel))                              .setOnClickListener(new View.OnClickListener() {                                  public void onClick(View v) {                                      positiveButtonClickListener.onClick(dialog,                                              DialogInterface.BUTTON_POSITIVE);                                  }                              });                  }              } else {                  // if no confirm button just set the visibility to GONE                  layout.findViewById(R.id.negativeButton_cancel).setVisibility(                          View.GONE);              }              // set the cancel button              if (negativeButtonText != null) {                  ((Button) layout.findViewById(R.id.negativeButton_cancel))                          .setText(negativeButtonText);                  if (negativeButtonClickListener != null) {                      ((Button) layout.findViewById(R.id.negativeButton_cancel))                              .setOnClickListener(new View.OnClickListener() {                                  public void onClick(View v) {                                      negativeButtonClickListener.onClick(dialog,                                              DialogInterface.BUTTON_NEGATIVE);                                  }                              });                  }              } else {                  // if no confirm button just set the visibility to GONE                  layout.findViewById(R.id.negativeButton_cancel).setVisibility(                          View.GONE);              }              // set the content message              if (message != null) {                  ((TextView) layout.findViewById(R.id.message)).setText(message);              } else if (contentView != null) {                  // if no message set                  // add the contentView to the dialog body                  ((LinearLayout) layout.findViewById(R.id.content))                          .removeAllViews();                  ((LinearLayout) layout.findViewById(R.id.content))                          .addView(contentView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));              }              dialog.setContentView(layout);              return dialog;          }      } }

關(guān)于使用上述控件如下所示:  

private void initDialogViews(){    MapDialog.Builder builder = new MapDialog.Builder(this);          builder.setMessage("這個(gè)就是自定義的提示框");          builder.setTitle("提示");          builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {              public void onClick(DialogInterface dialog, int which) {                  dialog.dismiss();                  //設(shè)置你的操作事項(xiàng)              }          });            builder.setNegativeButton("取消",                  new android.content.DialogInterface.OnClickListener() {                      public void onClick(DialogInterface dialog, int which) {                          dialog.dismiss();                      }                  });            builder.create().show();    }


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 拜泉县| 锦州市| 辽阳市| 都匀市| 钦州市| 巴林右旗| 德阳市| 如皋市| 左贡县| 池州市| 桃源县| 勐海县| 宜丰县| 驻马店市| 深州市| 凉山| 稻城县| 巫溪县| 会昌县| 天镇县| 竹溪县| 卢湾区| 若羌县| 元阳县| 巢湖市| 山丹县| 太仆寺旗| 当雄县| 中西区| 都匀市| 名山县| 乾安县| 高陵县| 阿勒泰市| 炉霍县| 渭南市| 五河县| 井冈山市| 永城市| 建平县| 申扎县|