您的位置:首页 > 移动开发 > Android开发

Android中AlertDialog对话框的使用

2015-02-02 22:49 316 查看
这次案例将常见的几种AlertDialog的使用集成到一个项目中,接下来给出该案例的实现代码:

MainActivity.java

public class MainActivity extends Activity implements OnClickListener {

private EditText mEditText;
private Button mButton1, mButton2, mButton3, mButton4, mButton5, mButton6;
final int SINGLE_DIALOG = 0X110;
final int MULTIPLE_DIALOG = 0X111;
final int CUSTOM_DIALOG = 0X112;
final int COMMON_CUSTOM_DIALOG = 0X113;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mEditText = (EditText) findViewById(R.id.edit);
initComponent();
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn1:
showCommonDialog();
break;
case R.id.btn2:
showListDialog();
break;
case R.id.btn3:
showDialog(SINGLE_DIALOG);
break;
case R.id.btn4:
showDialog(MULTIPLE_DIALOG);
break;
case R.id.btn5:
showDialog(CUSTOM_DIALOG);
break;
case R.id.btn6:
showDialog(COMMON_CUSTOM_DIALOG);
break;

default:
break;
}
}

/**
* 显示普通对话框
*/
private void showCommonDialog() {

Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("自定义普通对话框");
builder.setMessage("一个简单的题是对话框");
builder.setPositiveButton("确定", new AlertDialog.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
mEditText.setText("用户点击了PositiveButton");
}
});

builder.setNegativeButton("取消", new AlertDialog.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
mEditText.setText("用户点击了NegativeButton");
}
});
builder.setNeutralButton("中立", new AlertDialog.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
mEditText.setText("用户点击了NeutralButton");
}
});
builder.create().show();
}

/**
* 显示列表对话框
*/
private void showListDialog() {
Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("简单列表对话框");
builder.setItems(new String[] { "红色", "绿色", "蓝色", "白色" },
new AlertDialog.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
switch (arg1) {
case 0:
mEditText.setBackgroundColor(Color.RED);
break;
case 1:
mEditText.setBackgroundColor(Color.GREEN);
break;
case 2:
mEditText.setBackgroundColor(Color.BLUE);
break;
case 3:
mEditText.setBackgroundColor(Color.WHITE);
break;
default:
break;
}
}
});
builder.create().show();
}

/**
* 显示单选列表对话框
*/
@SuppressWarnings("deprecation")
private void showSingleDialog() {
Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("单选列表对话框");
builder.setSingleChoiceItems(new String[] { "红色", "绿色", "蓝色", "白色" },
3, new AlertDialog.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
switch (arg1) {
case 0:
mEditText.setBackgroundColor(Color.RED);
break;
case 1:
mEditText.setBackgroundColor(Color.GREEN);
break;
case 2:
mEditText.setBackgroundColor(Color.BLUE);
break;
case 3:
mEditText.setBackgroundColor(Color.WHITE);
break;
default:
break;
}
}
});
builder.setPositiveButton("确定", null);
builder.create().show();
}

/**
* 显示多选列表对话框
*/
@SuppressWarnings("deprecation")
private void showMultipleDialog() {
final String[] colorName = { "红色", "蓝色", "绿色" };
final boolean[] checkStatus = { false, false, false };
Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("选择您喜欢的颜色:");
builder.setMultiChoiceItems(colorName, checkStatus,
new AlertDialog.OnMultiChoiceClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1,
boolean arg2) {
// TODO Auto-generated method stub
String result = "您喜欢的颜色是";
for (int i = 0; i < checkStatus.length; i++) {
if (checkStatus[i]) {
result += colorName[i] + " ";
}
}
mEditText.setText(result);
}
});
builder.setPositiveButton("确定", null);
builder.create().show();
}

/**
* 显示自定义列表对话框
*/
private void showCustomDialog() {
// TODO Auto-generated method stub
final String[] names = { "神族", "虫族", "人族" };
int[] imagesId = { R.drawable.p, R.drawable.z, R.drawable.t };
List<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();
for (int i = 0; i < imagesId.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", imagesId[i]);
map.put("name", names[i]);
itemList.add(map);
}
SimpleAdapter adapter = new SimpleAdapter(this, itemList,
R.layout.item, new String[] { "image", "name" }, new int[] {
R.id.image, R.id.name });

Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("选择您喜欢的兵种:");
builder.setAdapter(adapter, new AlertDialog.OnClickListener() {

@Override
public void onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
mEditText.setText(names[arg1]);
}
});
builder.setPositiveButton("确定", null);
builder.create().show();
}

/**
* 显示普通自定义对话框
*/
private void showCommonCustomDialog() {
// TODO Auto-generated method stub
Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("请注册");
TableLayout login = (TableLayout) LayoutInflater.from(this).inflate(
R.layout.login, null);
builder.setView(login);
builder.setPositiveButton("确定", null);
builder.setNegativeButton("取消", null);
builder.create().show();
}

@Override
@Deprecated
protected Dialog onCreateDialog(int id, Bundle args) {
// TODO Auto-generated method stub
switch (id) {
case SINGLE_DIALOG:
showSingleDialog();
break;
case MULTIPLE_DIALOG:
showMultipleDialog();
break;
case CUSTOM_DIALOG:
showCustomDialog();
break;
case COMMON_CUSTOM_DIALOG:
showCommonCustomDialog();
break;
default:
break;
}
return super.onCreateDialog(id, args);
}

private void initComponent() {
mButton1 = (Button) findViewById(R.id.btn1);
mButton2 = (Button) findViewById(R.id.btn2);
mButton3 = (Button) findViewById(R.id.btn3);
mButton4 = (Button) findViewById(R.id.btn4);
mButton5 = (Button) findViewById(R.id.btn5);
mButton6 = (Button) findViewById(R.id.btn6);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
mButton3.setOnClickListener(this);
mButton4.setOnClickListener(this);
mButton5.setOnClickListener(this);
mButton6.setOnClickListener(this);

}
}
再给出主布局文件:(下面的TextView内容是我对AlertDialog使用的一个总结,写在String.xml中,由于内容比较多,所以用ScrollView包裹起来)

<?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" >

<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:editable="false" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:id="@+id/btn1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/button" />
<Button
android:id="@+id/btn2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/button" />
<Button
android:id="@+id/btn3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/button" />
<Button
android:id="@+id/btn4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/button" />
<Button
android:id="@+id/btn5"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/button" />
<Button
android:id="@+id/btn6"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/button" />
</LinearLayout>

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/content"
android:textSize="35sp" />
</ScrollView>

</LinearLayout>


实现效果







内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: