Android进行交互,对话框可谓一利器。故对于掌握各种对话框代码的编写是修炼必经之路。本人刚学Android编程不久,希望和大家交流,有何建议,请各位不吝赐教,不胜感激。废话少说,马上和大家一起总结AlertDialog的用法。

Android中创建对话框可以使用AlertDialog.Bulider类,还可以自定义对话框。对话框中还可以设置按钮并且监听它。

这两种方法等下都包含在同一个Demo中。

使用AlertDialog创建对话框,让我们先了解一下里面的methods:

setIcon() 给对话框设置图标
setTitle() 给对话框设置标题
setMessage() 给对话框设置设置提示信息
setItem() 给对话框设置显示一个list
setSingleChoiceItems() 给对话框设置一个单选列表
setMultiChoiceItems() 给对话框设置一个复选框按钮
setPositiveButton() 给对话框设置确定按钮
setNegativeButton() 给对话框设置取消按钮
setNeutralButton() 给对话框设置中间按钮

注意:这么方法我没带参数,等下直接看代码。

Ok 我们来看看这个Demo运行结果,有图有真相嘛

好吧表示对话框太多了,我就不一一贴出来了哈 ,仅贴几张表示下哈。

下面看下框架:

直接看代码:

  

1.string.xml的编写

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="hello">Hello World, DialogDemoActivity!</string>
<string name="app_name">DialogDemo</string>
<string name="btn1_text">确定取消对话框</string>
<string name="btn2_text">多个按钮信息框</string>
<string name="btn3_text">列表对话框</string>
<string name="btn4_text">进度条框</string>
<string name="btn5_text">单选选择对话框</string>
<string name="btn6_text">多向选择对话框</string>
<string name="btn7_text">自定义布局对话框</string>
<string name="btn8_text">读取进度对话框</string>

<string name="tvText_name">姓名</string>
<string name="tvText_password">密码</string>
</resources>

2.main.xml的编写

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="
http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn1_text"/>
<Button
android:id="@+id/btn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn2_text"/>
<Button
android:id="@+id/btn3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn3_text"/>
<Button
android:id="@+id/btn4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn4_text"/>
<Button
android:id="@+id/btn5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn5_text"/>
<Button
android:id="@+id/btn6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn6_text"/>
<Button
android:id="@+id/btn7"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn7_text"/>
<Button
android:id="@+id/btn8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/btn8_text"/>
</LinearLayout>
</ScrollView>

这个是主布局文件。

3.到了Activity的编写啦

package qyl.dialog;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
* copyright 2012/3/7
* @author qq
* @version 1.0
* 对话框大集合
*/

public class DialogDemoActivity extends Activity
implements Runnable,OnClickListener{
//确定取消信息对话框
private static final int DIALOG_0=1;
//多个按钮信息框
private static final int DIALOG_1=2;
//列表对话框
private static final int DIALOG_2=3;
//进度条框
private static final int DIALOG_3=4;
//单选选择框
private static final int DIALOG_4=5;
//多向选择框
private static final int DIALOG_5=6;
//自定义布局对话框
private static final int DIALOG_6=7;
//读取进度对话框
private static final int DIALOG_7=8;

int [] buttonIds={
R.id.btn1,R.id.btn2,R.id.btn3,R.id.btn4,
R.id.btn5,R.id.btn6,R.id.btn7,R.id.btn8
};

Button []btns=new Button[8];

String [] items={
"qq","qyl","happy","home",
"name","adress","number","hello"
};

int MAX=100;
ProgressDialog pd=null;
int choiceId=-1;

ArrayList <Integer> multiChoiceId=new ArrayList <Integer>();

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//引用布局文件并设置监听器
findViews();
}


private void findViews(){
for(int i=0;i<btns.length;i++){
btns[i]=(Button)findViewById(buttonIds[i]);
btns[i].setOnClickListener(this);

}
}


public void createDialog(int id){
AlertDialog.Builder bulider=new AlertDialog.Builder(this);
switch(id){
case DIALOG_0:
Log.d("ID", id+"");
bulider.setIcon(R.drawable.ic_launcher)
.setTitle("你确定喜欢我?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

//这种格式是不是看了眼花,可读性不好?至少我是这么认为的
showDialog("你选择了确定");
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
showDialog("你选择了取消");
}
}).create().show();
break;

case DIALOG_1:

//换种是不是感觉小清新啦
bulider.setIcon(R.drawable.ic_launcher);
bulider.setTitle("投票");
bulider.setMessage("你喜欢什么运动");
bulider.setPositiveButton("篮球", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
showDialog("你选择了篮球");
}
});
bulider.setNeutralButton("足球",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
showDialog("你选择了足球");
}
});
bulider.setNegativeButton("排球",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
showDialog("你选择了排球");
}
});
bulider.create().show();
break;

case DIALOG_2:
bulider.setTitle("列表选择框");
bulider.setItems(items, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//点击后选择弹出选择的第几项
showDialog("你选择的ID为 "+which+","+items[which]);
}
});
bulider.create().show();
break;

case DIALOG_3:
pd=new ProgressDialog(this);
pd.setIcon(R.drawable.ic_launcher);
pd.setTitle("进度条窗口");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMax(MAX);
pd.setButton("确定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//这里暂不需实现
}
});
pd.setButton2("取消", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//这里暂不需实现
}
});
pd.show();
new Thread(this).start();
break;

case DIALOG_4:
bulider.setIcon(R.drawable.ic_launcher);
bulider.setTitle("单项选择");
bulider.setSingleChoiceItems(items, 0,
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
choiceId=which;
showDialog("你选择的Id是"+which+","+items[which]);
}
});
bulider.setPositiveButton("确定",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
if(choiceId>0){
showDialog("你选择的是"+choiceId);
}
}
});
bulider.setNegativeButton("取消",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//这里暂不需要实现
}
});
bulider.create().show();
break;

case DIALOG_5:
multiChoiceId.clear();
bulider.setIcon(R.drawable.ic_launcher);
bulider.setTitle("多项选择");
bulider.setMultiChoiceItems(items,
new boolean[]{false,false,false,false,
false,false,false,false},
new DialogInterface.OnMultiChoiceClickListener() {

@Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
multiChoiceId.add(which);
showDialog("你选择的Id是"+which+","+items[which]);
}
else{
multiChoiceId.remove(which);
}
}
}
);
bulider.setPositiveButton("确定",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String str="";
int size=multiChoiceId.size();
for(int i=0;i<size;i++){
str+=items[multiChoiceId.get(i)]+", ";
}
showDialog("你选择的是"+str);
}
});
bulider.setNegativeButton("取消",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//这里暂不用写
}
});
bulider.create().show();
break;

case DIALOG_6:
LayoutInflater factory=LayoutInflater.from(this);
final View textEntryView=factory.inflate(R.layout.inflater, null);
bulider.setIcon(R.drawable.ic_launcher);
bulider.setTitle("自定义输入框");
bulider.setView(textEntryView);
bulider.setPositiveButton("确定",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//注意要加上textEntryView,默认的是main的布局视图
EditText name=(EditText)textEntryView.findViewById(R.id.etName);
EditText passWord=(EditText)textEntryView.findViewById(R.id.etPassword);
Log.d("name", name.getText().toString()+"");

showDialog("姓名:"+name.getText().toString()+
"密码:"+passWord.getText().toString());
}
});
bulider.setNegativeButton("取消",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//暂不用处理
}
});
bulider.create().show();
break;

case DIALOG_7:
pd=new ProgressDialog(this);
pd.setTitle("读取中……");
pd.setMessage("正在读取中,请稍后……");
pd.setIndeterminate(true);
pd.setCancelable(true);
pd.show();
return;

}
}


public void showDialog(String str){
Toast.makeText(this, str, Toast.LENGTH_LONG)
.show();
}

@Override
public void run() {
// TODO Auto-generated method stub
int pregress=0;
while(pregress<MAX){
try{
Thread.sleep(100);
pregress++;
pd.incrementProgressBy(1);
}
catch(InterruptedException ie){
ie.printStackTrace();
}
}
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btn1:
Log.d("Dialog1", v.getId()+"");
createDialog(DIALOG_0);
break;
case R.id.btn2:
Log.d("Dialog2", v.getId()+"");
createDialog(DIALOG_1);
break;
case R.id.btn3:
Log.d("Dialog3", v.getId()+"");
createDialog(DIALOG_2);
break;
case R.id.btn4:
Log.d("Dialog4", v.getId()+"");
createDialog(DIALOG_3);
break;
case R.id.btn5:
Log.d("Dialog5", v.getId()+"");
createDialog(DIALOG_4);
break;
case R.id.btn6:
Log.d("Dialog6", v.getId()+"");
createDialog(DIALOG_5);
break;
case R.id.btn7:
Log.d("Dialog7", v.getId()+"");
createDialog(DIALOG_6);
break;
case R.id.btn8:
Log.d("Dialog8", v.getId()+"");
createDialog(DIALOG_7);
break;
}
}
}

4.现在还差那个inflater.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="
http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableRow >
<TextView
android:id="@+id/tvText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvText_name"/>
<EditText
android:id="@+id/etName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minWidth="200dip"/>
</TableRow>
<TableRow >
<TextView
android:id="@+id/tvText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tvText_password"/>
<EditText
android:id="@+id/etPassword"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minWidth="200dip"
android:password="true"/>
</TableRow>

</TableLayout>

整体代码已经贴上,这次是第一次写这个,其中借鉴了别人的。希望与各位交流,如果还有其他好方法那就雁过留毛啊。

附:资源下载地址:http://download.csdn.net/my

更多相关文章

  1. android EditText设置不可写
  2. Android(安卓)环境搭建
  3. android studio调试c/c++代码
  4. android“设置”里的版本号
  5. 在Fragment中设置控件点击方法,执行失败。
  6. android拍照与读取相册
  7. Android(安卓)闹钟管理类的使用
  8. Android(安卓)报错:Caused by: android.os.FileUriExposedExcepti
  9. Android设置通知栏/状态栏透明改变通知栏颜色和app最上部分颜色

随机推荐

  1. Android串口通信封装之OkUSB
  2. 【笔记】关于java.exe finished with non
  3. requires compiler compliance level 5.0
  4. SQLite数据库增删改查操作
  5. Unity3d获取persistentDataPath返回空的
  6. Fragment生命周期及基本使用
  7. Android腾讯微薄客户端开发六:给用户加VI
  8. Android调用第三方app(Scheme隐式以及显
  9. INSTALL_FAILED_TEST_ONLY的原因
  10. 项目运行报错Error: Static interface me