激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Android - Android Dialog仿ios9中UIAlertController控件

Android Dialog仿ios9中UIAlertController控件

2022-03-06 20:28lady_zhou Android

這篇文章主要為大家詳細(xì)介紹了Android Dialog仿ios9中UIAlertController控件,具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近因為項目需要,用Android Dialog仿ios9中的UIAlertController控件,第一次對自定義控件進(jìn)行封裝,請大家多多指教

Android Dialog仿ios9中UIAlertController控件

如圖所示,當(dāng)我封裝的Dialog被觸發(fā)時,從底部彈出,點擊取消鍵整個彈框會消失,當(dāng)點擊不同的TextView會有相應(yīng)的點擊事件發(fā)生,目前只寫了三個能被點擊的TextView(以后會改為可以動態(tài)添加個數(shù))。
以下代碼是我封裝的BottomDialog:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
public class BottomDialog extends Dialog {
 private TextView mTitleTv;
 private TextView mOneTv;
 private TextView mTwoTv;
 private TextView mCancelTv;
 private Dialog myDialog;
 private ClickListenerInterface clickListenerInterface;
 
 public interface ClickListenerInterface {
  void onTitleClick();//點擊標(biāo)題TextView
 
  void onOneClick();//點擊第一個TextView
 
  void onTwoClick();//點擊第二個TextView
 }
 
 public BottomDialog(@NonNull Context context) {
  super(context);
  init();
 }
 
 public BottomDialog(@NonNull Context context, @StyleRes int themeResId) {
  super(context, themeResId);
  init();
 }
 
 protected BottomDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
  super(context, cancelable, cancelListener);
  init();
 }
 
 private void init() {
  myDialog = new Dialog(getContext(), R.style.BottomDialogStyle);
  //填充對話框的布局
  View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_bottom, null);
  //初始化控件
  mTitleTv = (TextView) view.findViewById(R.id.tv_dialog_title);
  mOneTv = (TextView) view.findViewById(R.id.tv_dialog_one);
  mTwoTv = (TextView) view.findViewById(R.id.tv_dialog_two);
  mCancelTv = (TextView) view.findViewById(R.id.tv_dialog_cancel);
 
  mTitleTv.setOnClickListener(new DialogClickListener());
  mOneTv.setOnClickListener(new DialogClickListener());
  mTwoTv.setOnClickListener(new DialogClickListener());
  mCancelTv.setOnClickListener(new DialogClickListener());
 
  //將布局設(shè)置給Dialog
  myDialog.setContentView(view);
  //獲取當(dāng)前Activity所在的窗體
  Window dialogWindow = myDialog.getWindow();
  //設(shè)置Dialog從窗體底部彈出
  dialogWindow.setGravity(Gravity.BOTTOM);
 
  //獲得窗體的屬性
  WindowManager.LayoutParams lp = dialogWindow.getAttributes();
  lp.width = (int) (dialogWindow.getWindowManager().getDefaultDisplay().getWidth() * 0.95);
  lp.y = 20; //設(shè)置Dialog距離底部的距離
  dialogWindow.setAttributes(lp); //將屬性設(shè)置給窗體
  myDialog.show();//顯示對話框
 }
 
 public void setClicklistener(ClickListenerInterface clickListenerInterface) {
  this.clickListenerInterface = clickListenerInterface;
 }
 
 private class DialogClickListener implements View.OnClickListener {
  @Override
  public void onClick(View v) {
   switch (v.getId()) {
    case R.id.tv_dialog_title:
     clickListenerInterface.onTitleClick();
     break;
    case R.id.tv_dialog_one:
     clickListenerInterface.onOneClick();
     break;
    case R.id.tv_dialog_two:
     clickListenerInterface.onTwoClick();
     break;
    case R.id.tv_dialog_cancel:
     myDialog.dismiss();
     break;
   }
  }
 }
 
 public void dismissDialog() {
  if (myDialog != null && myDialog.isShowing()) {
   myDialog.dismiss();
  }
 }
 
 /**
  * 設(shè)置標(biāo)題欄文本文字
  *
  * @param stringId
  * @see #setTitleText(String)
  */
 public void setTitleText(@StringRes int stringId) {
  setTitleText(getContext().getString(stringId));
 }
 
 /**
  * 設(shè)置標(biāo)題欄文本文字
  *
  * @param text
  */
 public void setTitleText(String text) {
  mTitleTv.setText(text);
  mTitleTv.setVisibility(View.VISIBLE);
 }
 
 /**
  * 設(shè)置第一個TextView文字
  *
  * @param stringId
  */
 public void setOneText(@StringRes int stringId) {
  setOneText(getContext().getString(stringId));
 }
 
 /**
  * 設(shè)置第一個TextView文字
  *
  * @param text
  */
 public void setOneText(String text) {
  mOneTv.setText(text);
  mOneTv.setVisibility(View.VISIBLE);
 }
 
 /**
  * 設(shè)置第二個TextView文字
  *
  * @param stringId
  */
 public void setTwoText(@StringRes int stringId) {
  setTwoText(getContext().getString(stringId));
 }
 
 /**
  * 設(shè)置第二個TextView文字
  *
  * @param text
  */
 public void setTwoText(String text) {
  mTwoTv.setText(text);
  mTwoTv.setVisibility(View.VISIBLE);
 }
 
 /**
  * 獲取標(biāo)題欄文本
  *
  * @return
  */
 public final TextView getTitleTv() {
  return mTitleTv;
 }
 
 /**
  * 獲取第一個文本
  *
  * @return
  */
 public final TextView getOneTv() {
  return mOneTv;
 }
 
 /**
  * 獲取第二個文本
  *
  * @return
  */
 public final TextView getTwoTv() {
  return mTwoTv;
 }
 
 
 /**
  * 設(shè)置字體顏色
  *
  * @param titleColor 標(biāo)題的顏色,默認(rèn)是灰色
  * @param otherColor 其他的顏色,默認(rèn)是藍(lán)色
  * @param i   有2種模式,1(標(biāo)題和其他顏色不一樣)2(標(biāo)題和其他顏色一樣,取消鍵不一樣)
  */
 public void setColor(int titleColor, int otherColor, int i) {
  if (i == 1) {
   mTitleTv.setTextColor(ContextCompat.getColor(getContext(), titleColor));
   mOneTv.setTextColor(ContextCompat.getColor(getContext(), otherColor));
   mTwoTv.setTextColor(ContextCompat.getColor(getContext(), otherColor));
  }
  if (i == 2) {
   mTitleTv.setTextColor(ContextCompat.getColor(getContext(), titleColor));
   mOneTv.setTextColor(ContextCompat.getColor(getContext(), titleColor));
   mTwoTv.setTextColor(ContextCompat.getColor(getContext(), titleColor));
   mCancelTv.setTextColor(ContextCompat.getColor(getContext(), otherColor));
  }
 }
 
 @Override
 public void onProvideKeyboardShortcuts(List<KeyboardShortcutGroup> data, @Nullable Menu menu, int deviceId) {
 
 }
}

在Activity中的應(yīng)用:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 private BottomDialog bottomDialog;
 private Button mButton;
 private TextView mTextView;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 mButton = (Button) findViewById(R.id.button);
 mTextView = (TextView) findViewById(R.id.textView);
 
 mButton.setOnClickListener(this);
 }
 
 @Override
 public void onClick(View v) {
 switch (v.getId()) {
  case R.id.button:
   showBottomDialog();
  break;
 }
 }
 
 /**
 * 展示
 */
 private void showBottomDialog() {
 bottomDialog = new BottomDialog(this);
 bottomDialog.setClicklistener(new BottomDialog.ClickListenerInterface() {
  @Override
  public void onTitleClick() {
 
  }
 
  @Override
  public void onOneClick() {
  mTextView.setText(bottomDialog.getOneTv().getText().toString());
  }
 
  @Override
  public void onTwoClick() {
  mTextView.setText(bottomDialog.getTwoTv().getText().toString());
  }
 });
 }
}


被封裝的Dialog主要提供的方法有:修改相應(yīng)的TextView文字和顏色,提供點擊相應(yīng)的TextView的點擊事件。

這里的點擊事件主要用的是回調(diào)的方法,在Dialog中定義一個接口,在點擊事件調(diào)用這個接口,當(dāng)在Activity中觸發(fā)這個回調(diào)接口時,點擊事件產(chǎn)生。

主要代碼是:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
private ClickListenerInterface clickListenerInterface;
 
public interface ClickListenerInterface {
 void onTitleClick();//點擊標(biāo)題TextView
 
 void onOneClick();//點擊第一個TextView
 
 void onTwoClick();//點擊第二個TextView
}
 
 public void setClicklistener(ClickListenerInterface clickListenerInterface) {
 this.clickListenerInterface = clickListenerInterface;
}
 
private class DialogClickListener implements View.OnClickListener {
 @Override
 public void onClick(View v) {
  switch (v.getId()) {
   case R.id.tv_dialog_title:
    clickListenerInterface.onTitleClick();
    break;
   case R.id.tv_dialog_one:
    clickListenerInterface.onOneClick();
    break;
   case R.id.tv_dialog_two:
    clickListenerInterface.onTwoClick();
    break;
   case R.id.tv_dialog_cancel:
    myDialog.dismiss();
    break;
  }
 }
}

最后說一下更改文字顏色的方法:

?
1
public void setColor(int titleColor, int otherColor, int i) {}

第一個傳的顏色是修改最上面的TextView,第二個傳的顏色是修改其他的文字顏色,第三個參數(shù)是表明你選用哪種模式,分別傳1或2,有2種模式

  1. 標(biāo)題和其他顏色不一樣
  2. 標(biāo)題和其他顏色一樣,取消鍵不一樣

下面是我的Github地址

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://blog.csdn.net/lady_zhou/article/details/75213800

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 毛片午夜 | 成年毛片 | 亚洲精品成人久久久 | 成人免费久久 | 亚洲第一激情 | 91九色免费视频 | 国产一级大片 | 成人精品一区二区 | 视频二区国产 | 成人黄色免费电影 | 午夜男人在线观看 | 精品国产91久久久久久久妲己 | 想要xx在线观看 | 黄视频在线网站 | 91久久久久久久一区二区 | 日本逼逼视频 | 中文在线日韩 | 中国性xxx | 麻豆视频在线免费观看 | 亚洲成人高清在线观看 | 欧美爱爱视频 | 污版视频在线观看 | 久久久www成人免费精品 | 91免费大片 | 91精品国产91久久久久久蜜臀 | av老司机久久 | 日本在线看 | 欧美特黄a | 国产成人精品免高潮在线观看 | 黑人日比| 国产午夜三级一区二区三桃花影视 | 亚洲第五色综合网 | 国产精品久久国产精麻豆96堂 | 一级做a在线观看 | 一级毛片手机在线观看 | 宅男视频在线观看免费 | 久久国产精品久久久久久电车 | 欧美成人免费电影 | 49vv看片免费 | 色七七亚洲 | 精品一区二区三区在线观看视频 |