1.常见对话框的源码:
- package com.naoh.stu;
- import java.util.ArrayList;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.ProgressDialog;
- import android.content.DialogInterface;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.Bundle;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup.LayoutParams;
- import android.view.WindowManager;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.PopupWindow;
- import android.widget.Toast;
- public class DiaAllActivity extends Activity implements Runnable {
- private Button btn_diaNormal;
- private Button btn_diaMulti;
- private Button btn_diaList;
- private Button btn_diaSinChos;
- private Button btn_diaMultiChos;
- private Button btn_diaProcess;
- private Button btn_diaReadProcess;
- private Button btn_diaCustom;
- private Button btn_popUpDia;
- private PopupWindow window=null;
- private Button cusPopupBtn1;
- private View popupView;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.dialog);
- getView();
- setListener();
- }
- private void getView()
- {
- btn_diaNormal=(Button)findViewById(R.id.btn_diaNormal);
- btn_diaMulti=(Button)findViewById(R.id.btn_diaMulti);
- btn_diaList=(Button)findViewById(R.id.btn_diaList);
- btn_diaSinChos=(Button)findViewById(R.id.btn_diaSigChos);
- btn_diaMultiChos=(Button)findViewById(R.id.btn_diaMultiChos);
- btn_diaProcess=(Button)findViewById(R.id.btn_diaProcess);
- btn_diaReadProcess=(Button)findViewById(R.id.btn_diaReadProcess);
- btn_diaCustom=(Button)findViewById(R.id.btn_diaCustom);
- btn_popUpDia=(Button)findViewById(R.id.btn_popUpDia);
- }
- private void setListener()
- {
- btn_diaNormal.setOnClickListener(btnListener);
- btn_diaMulti.setOnClickListener(btnListener);
- btn_diaList.setOnClickListener(btnListener);
- btn_diaSinChos.setOnClickListener(btnListener);
- btn_diaMultiChos.setOnClickListener(btnListener);
- btn_diaProcess.setOnClickListener(btnListener);
- btn_diaReadProcess.setOnClickListener(btnListener);
- btn_diaCustom.setOnClickListener(btnListener);
- btn_popUpDia.setOnClickListener(btnListener);
- }
- private Button.OnClickListener btnListener= new Button.OnClickListener()
- {
- public void onClick(View v)
- {
- if(v instanceof Button)
- {
- int btnId=v.getId();
- switch(btnId)
- {
- case R.id.btn_diaNormal:
- showNormalDia();
- break;
- case R.id.btn_diaMulti:
- showMultiDia();
- break;
- case R.id.btn_diaList:
- showListDia();
- break;
- case R.id.btn_diaSigChos:
- showSinChosDia();
- break;
- case R.id.btn_diaMultiChos:
- showMultiChosDia();
- break;
- case R.id.btn_diaReadProcess:
- showReadProcess();
- break;
- case R.id.btn_diaProcess:
- showProcessDia();
- break;
- case R.id.btn_diaCustom:
- showCustomDia();
- break;
- case R.id.btn_popUpDia:
- showCusPopUp(v);
- break;
- default:
- break;
- }
- }
- }
- };
- /*普通的对话框*/
- private void showNormalDia()
- {
- //AlertDialog.Builder normalDialog=new AlertDialog.Builder(getApplicationContext());
- AlertDialog.Builder normalDia=new AlertDialog.Builder(DiaAllActivity.this);
- normalDia.setIcon(R.drawable.ic_launcher);
- normalDia.setTitle("普通的对话框");
- normalDia.setMessage("普通对话框的message内容");
- normalDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- showClickMessage("确定");
- }
- });
- normalDia.setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- showClickMessage("取消");
- }
- });
- normalDia.create().show();
- }
- /*多按钮对话框*/
- private void showMultiDia()
- {
- AlertDialog.Builder multiDia=new AlertDialog.Builder(DiaAllActivity.this);
- multiDia.setTitle("多选项对话框");
- multiDia.setPositiveButton("按钮一", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- showClickMessage("按钮一");
- }
- });
- multiDia.setNeutralButton("按钮二", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- showClickMessage("按钮二");
- }
- });
- multiDia.setNegativeButton("按钮三", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- showClickMessage("按钮三");
- }
- });
- multiDia.create().show();
- }
- /*列表对话框*/
- private void showListDia()
- {
- final String[] mList={"选项1","选项2","选项3","选项4","选项5","选项6","选项7"};
- AlertDialog.Builder listDia=new AlertDialog.Builder(DiaAllActivity.this);
- listDia.setTitle("列表对话框");
- listDia.setItems(mList, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- /*下标是从0开始的*/
- showClickMessage(mList[which]);
- }
- });
- listDia.create().show();
- }
- /*单项选择对话框*/
- int yourChose=-1;
- private void showSinChosDia()
- {
- final String[] mList={"选项1","选项2","选项3","选项4","选项5","选项6","选项7"};
- yourChose=-1;
- AlertDialog.Builder sinChosDia=new AlertDialog.Builder(DiaAllActivity.this);
- sinChosDia.setTitle("单项选择对话框");
- sinChosDia.setSingleChoiceItems(mList, 0, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- yourChose=which;
- }
- });
- sinChosDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- if(yourChose!=-1)
- {
- showClickMessage(mList[yourChose]);
- }
- }
- });
- sinChosDia.create().show();
- }
- ArrayList<Integer> myChose= new ArrayList<Integer>();
- private void showMultiChosDia()
- {
- final String[] mList={"选项1","选项2","选项3","选项4","选项5","选项6","选项7"};
- final boolean mChoseSts[]={false,false,false,false,false,false,false};
- myChose.clear();
- AlertDialog.Builder multiChosDia=new AlertDialog.Builder(DiaAllActivity.this);
- multiChosDia.setTitle("多项选择对话框");
- multiChosDia.setMultiChoiceItems(mList, mChoseSts, new DialogInterface.OnMultiChoiceClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which, boolean isChecked) {
- // TODO Auto-generated method stub
- if(isChecked)
- {
- myChose.add(which);
- }
- else
- {
- myChose.remove(which);
- }
- }
- });
- multiChosDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- int size=myChose.size();
- String str="";
- for(int i=0;i<size;i++)
- {
- str+=mList[myChose.get(i)];
- }
- showClickMessage(str);
- }
- });
- multiChosDia.create().show();
- }
- //进度读取框需要模拟读取
- ProgressDialog mReadProcessDia=null;
- public final static int MAX_READPROCESS = 100;
- private void showReadProcess()
- {
- mReadProcessDia=new ProgressDialog(DiaAllActivity.this);
- mReadProcessDia.setProgress(0);
- mReadProcessDia.setTitle("进度条窗口");
- mReadProcessDia.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
- mReadProcessDia.setMax(MAX_READPROCESS);
- mReadProcessDia.show();
- new Thread(this).start();
- }
- //新开启一个线程,循环的累加,一直到100然后在停止
- @Override
- public void run()
- {
- int Progress= 0;
- while(Progress < MAX_READPROCESS)
- {
- try {
- Thread.sleep(100);
- Progress++;
- mReadProcessDia.incrementProgressBy(1);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //读取完了以后窗口自消失
- mReadProcessDia.cancel();
- }
- /*读取中的对话框*/
- private void showProcessDia()
- {
- ProgressDialog processDia= new ProgressDialog(DiaAllActivity.this);
- processDia.setTitle("进度条框");
- processDia.setMessage("内容读取中...");
- processDia.setIndeterminate(true);
- processDia.setCancelable(true);
- processDia.show();
- }
- /*自定义对话框*/
- private void showCustomDia()
- {
- AlertDialog.Builder customDia=new AlertDialog.Builder(DiaAllActivity.this);
- final View viewDia=LayoutInflater.from(DiaAllActivity.this).inflate(R.layout.custom_dialog, null);
- customDia.setTitle("自定义对话框");
- customDia.setView(viewDia);
- customDia.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- EditText diaInput=(EditText) viewDia.findViewById(R.id.txt_cusDiaInput);
- showClickMessage(diaInput.getText().toString());
- }
- });
- customDia.create().show();
- }
- /*popup window 来实现*/
- private void showCusPopUp(View parent)
- {
- if(window == null)
- {
- popupView=LayoutInflater.from(DiaAllActivity.this).inflate(R.layout.dia_cuspopup_dia, null);
- cusPopupBtn1=(Button)popupView.findViewById(R.id.diaCusPopupSure);
- window =new PopupWindow(popupView,LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
- }
- window.setAnimationStyle(R.style.PopupAnimation);
- /*必须调用setBackgroundDrawable, 因为popupwindow在初始时,会检测background是否为null,如果是onTouch or onKey events就不会相应,所以必须设置background*/
- /*网上也有很多人说,弹出pop之后,不响应键盘事件了,这个其实是焦点在pop里面的view去了。*/
- window.setFocusable(true);
- window.setBackgroundDrawable(new BitmapDrawable());
- window.update();
- window.showAtLocation(parent, Gravity.CENTER_VERTICAL, 0, 0);
- cusPopupBtn1.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- showClickMessage("popup window的确定");
- }
- });
- }
- /*显示点击的内容*/
- private void showClickMessage(String message)
- {
- Toast.makeText(DiaAllActivity.this, "你选择的是: "+message, Toast.LENGTH_SHORT).show();
- }
- }
布局,就是一堆的button
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="各种Dialog合集" />
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="普通Dialog"
- android:id="@+id/btn_diaNormal"/>
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="多按钮Dialog"
- android:id="@+id/btn_diaMulti"/>
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="列表Dialog"
- android:id="@+id/btn_diaList"/>
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="单项选择Dialog"
- android:id="@+id/btn_diaSigChos"/>
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="多项选择Dialog"
- android:id="@+id/btn_diaMultiChos"/>
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="进度条Dialog"
- android:id="@+id/btn_diaReadProcess"/>
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="读取中Dialog"
- android:id="@+id/btn_diaProcess"/>
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="自定义Dialog"
- android:id="@+id/btn_diaCustom"/>
- <Button
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="PopUpWindow实现的dialog"
- android:id="@+id/btn_popUpDia"/>
- </LinearLayout>
2.自定义对话框:
系统默认的AlertDialog,与项目的UI不统一,所以,改了一下,定义了一样式,最终效果如下图:
另外,为了尽量少改原来的代码,该类类名及相关方法名都与android.app.AlertDialog相同,使用时,原代码只需要修改导入的包名,在按钮的Listener上加上一句关闭对话框的方法即可.
先是对话框的XML布局文件:
test.xml
1
2
3
4
5
6
7
8
9
10
11
|
<?
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:orientation
=
"vertical"
>
<!-- 顶部椭园边缘 -->
<
ImageView
android:layout_width
=
"400dp"
android:layout_height
=
"22dp"
android:src
=
"@drawable/dialog_top"
>
</
ImageView
>
<!-- 中间白色背景,两个TextView,标题和内容,留一个LinearLayout,在代码中根据调用动态加上按钮 -->
<
LinearLayout
android:layout_width
=
"400dp"
android:layout_height
=
"wrap_content"
android:background
=
"@drawable/dialog_center"
android:orientation
=
"vertical"
> <
TextView
android:textColor
=
"#000000"
android:textSize
=
"35dp"
android:gravity
=
"center"
android:id
=
"@+id/title"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
/> <
TextView
android:layout_marginLeft
=
"20dp"
android:layout_marginRight
=
"10dp"
android:id
=
"@+id/message"
android:layout_marginBottom
=
"10dp"
android:layout_marginTop
=
"20dp"
android:textColor
=
"#000000"
android:textSize
=
"25dp"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
/>
<!-- 在LinearLayout中加按钮 -->
<
LinearLayout
android:id
=
"@+id/buttonLayout"
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:layout_gravity
=
"center"
android:gravity
=
"center"
android:orientation
=
"horizontal"
> </
LinearLayout
>
</
LinearLayout
>
<!-- 底部椭园边缘 -->
<
ImageView
android:layout_marginTop
=
"-2dp"
android:layout_width
=
"400dp"
android:layout_height
=
"22dp"
android:src
=
"@drawable/dialog_bottom"
>
</
ImageView
> </
LinearLayout
>
|
按钮背景,不同状态不同,可参考本博相关文章:
alertdialog_button.xml
1
2
3
4
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?><
selector
xmlns:android
=
"http://schemas.android.com/apk/res/android"
>
<
item
android:state_pressed
=
"true"
android:drawable
=
"@drawable/dialog_button_pressed"
/>
<
item
android:state_focused
=
"true"
android:drawable
=
"@drawable/dialog_button_pressed"
/>
<
item
android:drawable
=
"@drawable/dialog_button"
/></
selector
>
|
下面就是自定义的AlertDialog类的代码:
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
|
public
class
AlertDialog {
Context context;
android.app.AlertDialog ad;
TextView titleView;
TextView messageView;
LinearLayout buttonLayout;
public
AlertDialog(Context context) {
// TODO Auto-generated constructor stub
this
.context=context;
ad=
new
android.app.AlertDialog.Builder(context).create();
ad.show();
//关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
Window window = ad.getWindow();
window.setContentView(R.layout.alertdialog);
titleView=(TextView)window.findViewById(R.id.title);
messageView=(TextView)window.findViewById(R.id.message);
buttonLayout=(LinearLayout)window.findViewById(R.id.buttonLayout);
}
public
void
setTitle(
int
resId)
{
titleView.setText(resId);
}
public
void
setTitle(String title) {
titleView.setText(title);
}
public
void
setMessage(
int
resId) {
messageView.setText(resId);
}
public
void
setMessage(String message)
{
messageView.setText(message);
}
/**
* 设置按钮
* @param text
* @param listener
*/
public
void
setPositiveButton(String text,
final
View.OnClickListener listener)
{
Button button=
new
Button(context);
LinearLayout.LayoutParams params=
new
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.alertdialog_button);
button.setText(text);
button.setTextColor(Color.WHITE);
button.setTextSize(
20
);
button.setOnClickListener(listener);
buttonLayout.addView(button);
}
/**
* 设置按钮
* @param text
* @param listener
*/
public
void
setNegativeButton(String text,
final
View.OnClickListener listener)
{
Button button=
new
Button(context);
LinearLayout.LayoutParams params=
new
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.alertdialog_button);
button.setText(text);
button.setTextColor(Color.WHITE);
button.setTextSize(
20
);
button.setOnClickListener(listener);
if
(buttonLayout.getChildCount()>
0
)
{
params.setMargins(
20
,
0
,
0
,
0
);
button.setLayoutParams(params);
buttonLayout.addView(button,
1
);
}
else
{
button.setLayoutParams(params);
buttonLayout.addView(button);
} }
/**
* 关闭对话框
*/
public
void
dismiss() {
ad.dismiss();
} }
|
使用方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
final
AlertDialog ad=
new
AlertDialog(Test.
this
);
ad.setTitle(
"标题"
);
ad.setMessage(
"内容sdfsafdasf内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容内容"
);
ad.setPositiveButton(
"确定"
,
new
OnClickListener() {
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
ad.dismiss();
Toast.makeText(Test.
this
,
"被点到确定"
, Toast.LENGTH_LONG).show();
}
});
ad.setNegativeButton(
"取消"
,
new
OnClickListener() {
@Override
public
void
onClick(View v) {
// TODO Auto-generated method stub
ad.dismiss();
Toast.makeText(Test.
this
,
"被点到取消"
, Toast.LENGTH_LONG).show();
}
});
|