原标题:Android 自定义的AlertDialog强化版
介绍了自定义的AlertDialog,在此基础之上,再来看下常用的自定义的AlertDialog。
ItemDialog:
首先来看下android原生的itemDialog
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
package com.example.yk.dialogtest;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Toast;
public class ItemDialogActivity extends AppCompatActivity {
private String[] items={ "a" , "b" , "c" , "d" , "e" , "d" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_item_dialog);
AlertDialog.Builder alertDialog= new AlertDialog.Builder( this );
alertDialog.setTitle( "title" );
alertDialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(ItemDialogActivity. this , "点击了" +items[i], Toast.LENGTH_SHORT).show();
}
});
alertDialog.show();
}
}
效果如图:
自定义的itemDialog:
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
package com.example.yk.dialogtest;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
/**
* Created by yk on 2017/1/17.
*/
public class itemDialog extends Dialog implements AdapterView.OnItemClickListener {
public interface OnDialogItemClickListener {
/**
* 点击item 事件的回调方法
* @param requestCode 用于区分某种情况下的showDialog
* @param position
* @param item
*/
void onDialogItemClick( int requestCode, int position,String item);
}
private String title;
private Context context;
private String[] items;
private int requestCode;
private OnDialogItemClickListener listener;
public itemDialog(Context context, String title, String[] items, int requestCode, OnDialogItemClickListener listener) {
super (context, R.style.MyDialog);
this .context=context;
this .title=title;
this .items=items;
this .requestCode=requestCode;
this .listener=listener;
}
private TextView textView;
private ListView listView;
private ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.item_dialog);
textView = (TextView) findViewById(R.id.tvItemDialogTitle);
listView = (ListView) findViewById(R.id.lvItemDialog);
adapter = new ArrayAdapter(context, R.layout.item_dialog_activity,R.id.text_view, items);
textView.setText(title);
listView.setAdapter(adapter);
listView.setOnItemClickListener( this );
}
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
listener.onDialogItemClick(requestCode,i, adapter.getItem(i));
dismiss();
}
}
效果图:
EditDialog:
代码太多,省略了,请点击原文链接查看吧。
效果图:
如果需要对editText输入的内容作限制,需要在EditDialog中添加如下代码:
1
2
CustomWatcher customWatcher= new CustomWatcher();
etInput.addTextChangedListener(customWatcher);
CustomWatcher:
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
private class CustomWatcher implements TextWatcher {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
String s = editable.toString();
/**
* 不以小数点开头
*/
if (s.length() == 1 && s.contains( "." )){
editable.delete( 0 , 1 );
return ;
}
/**
* 不能以00开头
*/
if (s.startsWith( "00" )){
editable.delete( 1 , 2 );
return ;
}
/**
* 整数部分最长为8位
*/
if (!s.contains( "." )){
if (s.length() == 9 ){
editable.delete( 8 , 9 );
}
return ;
}
/**
* 小数部分最多有2位
*/
if (s.contains( "." )){
int start = s.indexOf( "." )+ 1 ;
int end=s.length();
if (end-start> 2 ){
editable.delete(start+ 2 ,s.length());
}
return ;
}
}
}
效果截图:
LoadProgressDialog:
代码太多,省略了,请点击原文链接查看吧。
实现的效果:
在此基础之上,可以进一步添加自定义样式:
在LoadProgressDialog中改变的代码:
1
2
3
4
5
public LoadProgressDialog(Context context,String message, boolean canCancel) {
super (context,R.style.easy_dialog_style);
this .message=message;
this .canCancel=canCancel;
}
自定义样式R.style.easy_dialog_style为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
< style name = "easy_dialog_style" parent = "@android:style/Theme.Dialog" >
< item name = "android:windowFrame" >@null item >
< item name = "android:windowIsFloating" >true item >
< item name = "android:windowIsTranslucent" >true item >
< item name = "android:windowNoTitle" >true item >
< item name = "android:windowBackground" >@color/transparent item >
< item name = "android:backgroundDimEnabled" >true item >
style >
R.layout.layout_load_progress_dialog布局为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout xmlns:android = " "
android:layout_width = "280dp"
android:layout_height = "wrap_content"
android:background = "@drawable/edit_item_text_bg"
android:orientation = "vertical" >
< TextView
android:id = "@+id/tv_message"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:gravity = "center"
android:layout_marginTop = "5dp"
android:layout_marginBottom = "20dp"
android:textSize = "20sp" />
< ProgressBar
android:id = "@+id/progress_bar"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:paddingBottom = "10dp"
android:layout_gravity = "center"
android:indeterminateDrawable = "@drawable/custom_progress_style" />
LinearLayout >
drawable/custom_progress_style:
1
2
3
4
5
6
xml version = "1.0" encoding = "utf-8" ?>
< animated-rotate xmlns:android = " "
android:drawable = "@mipmap/load_dialog"
android:pivotX = "50%"
android:pivotY = "50%"
/>
效果:
控制LoadProgressDialog显示与不显示的工具类:
LoadProgressActivity中代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.example.yk.dialogtest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class LoadProgressActivity extends AppCompatActivity {
private LoadProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_load_progress);
// dialog = new LoadProgressDialog(this,"加载中",true);
// dialog.show();
LoadProgressUtil.showLoadProgressDialog( this , "加载中" , true );
}
}
LoadProgressUtil中代码:
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
package com.example.yk.dialogtest;
import android.content.Context;
import android.util.Log;
/**
* Created by yk on 2017/1/18.
*/
public class LoadProgressUtil {
private static LoadProgressDialog loadProgressDialog;
public static void showLoadProgressDialog(Context context,String title, boolean canCancel){
if (loadProgressDialog == null ){
loadProgressDialog= new LoadProgressDialog(context,title,canCancel);
} else {
if (loadProgressDialog.isShowing()){
return ;
}
}
// Log.e("context",context.toString());
// Log.e("loadProgressDialog",loadProgressDialog.getContext().toString());
loadProgressDialog.show();
}
public static void cancelProgressDialog(){
if (loadProgressDialog== null ){
return ;
} else {
if (loadProgressDialog.isShowing()){
loadProgressDialog.dismiss();
} else {
return ;
}
}
}
public static void progressDialogIsShow(){
loadProgressDialog.isShowing();
}
}
如果像上面这样写的话,会发现,在第一次取消progressDialog,并且退出当前progressDialog所在的Activity,当第二次进入该progressDialog所在的Activity时是会报错的,错误原因:
1
Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy @3d4e7354 is not valid; is your activity running?