您的位置:首页 > 产品设计 > UI/UE

AlertDialog和AlertDialog.builder(二)

2013-05-16 15:00 363 查看
1、列表对话框。

列表对话框的实现可以使用两种方式,一种可以使用硬编码的方法,另外一种是使用XML配置

第一种的情况需要定义一个显示数组,然后使用AlertDialog进行展示的时候使用数组里面的内容进行显示

首先还是需要在activity_main.xml里面定义两个组件

<TextView
android:id="@+id/showPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_phone"/>
<Button
android:id="@+id/showPhoneBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_phone_btn"/>


接着在MainActivity.java里面定义一个手机品牌的数组

// 喜欢的手机品牌
private String[] tel = new String[] { "MOTO", "SAMSUNG", "APPLE", "NOKIA",
"HTC", "MEIZU" };


以下为详细实现

// 取得组件
showPhone = (TextView) findViewById(R.id.showPhone);
showPhoneBtn = (Button) findViewById(R.id.showPhoneBtn);
// 设置监听
showPhoneBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("请选择您喜欢的品牌").setIcon(R.drawable.dialog)
// 这里使用setItems(items, listener)方法
.setItems(tel, new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
showPhone.setText("您喜欢的手机品牌是:" + tel[which]);

}
}).show();

}
});


需要说明的是,这里使用的是AlertDialog.builder里面的, android.content.DialogInterface.OnClickListener)]setItems(CharSequence[] items, DialogInterface.OnClickListener listener)方法,里面的参数也很简单,不需要再加以赘述。

下图为运行的结果图



选择后的结果



第二种方法是进行XML配置

首先需要建立一个相关的XML文件进行存放数据color.xml

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

<string-array name="color">
<item>红色</item>
<item>绿色</item>
<item>蓝色</item>
</string-array>

</resources>


然后在activity_main.xml里面定义两个组件

<TextView
android:id="@+id/showColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_color" />

<Button
android:id="@+id/showColorBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_color_btn" />


那么详细的代码实现为下

// 取得组件
showColor = (TextView) findViewById(R.id.showColor);
showColorBtn = (Button) findViewById(R.id.showColorBtn);

// 设置监听
showColorBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("请选择您喜欢的颜色")
.setIcon(R.drawable.dialog)
.setItems(R.array.color,
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which) {
// 取得资源的文件需要使用getResources()方法
showColor
.setText("您喜欢的颜色是:"
+ getResources()
.getStringArray(
R.array.color)[which]);
}
}).show();
}
});


运行结果如下



选择之后的结果



单选以及复选框

复选框的实现

同样的,首先先在activity_main.xml里面定义组件

<TextView
android:id="@+id/showBooks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_books" />

<Button
android:id="@+id/showBooksBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_books" />


然后定义一个显示的信息列表

// 书籍列表
private String[] books = new String[] { "Android开发", "thinking in java",
"android游戏", "SSH实战经典" };
// 相对应的boolean型(这里的数组内容多少必须跟书籍列表的相同)
// true表示默认选中,false表示不选中
private boolean[] booksChoice = new boolean[]{false,false,false,false};


以下为详细的实现代码

// 取得组件
showBooksBtn = (Button) findViewById(R.id.showBooksBtn);
showBooks = (TextView) findViewById(R.id.showBooks);
// 设置监听
showBooksBtn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
new AlertDialog.Builder(MainActivity.this)
.setTitle("请选择书籍")
.setIcon(R.drawable.dialog)
// 复选框
.setMultiChoiceItems(
// 列表数组
books,
// 是否选中
booksChoice,
new DialogInterface.OnMultiChoiceClickListener() {

@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
// 循环是否已被选中
for (int i = 0; i < books.length; i++) {
// 如果选中则打印
if (i == which && isChecked) {
showBooks.append(books[i]+"、");
}
}

}
// 确定按钮
}).setPositiveButton("确定", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

}
}).show();
}
});


, boolean[], android.content.DialogInterface.OnMultiChoiceClickListener)]setMultiChoiceItems(CharSequence[] items,
boolean[] checkedItems, DialogInterface.OnMultiChoiceClickListener listener)方法为设置

复选框,而, int, android.content.DialogInterface.OnClickListener)]setSingleChoiceItems(CharSequence[] items,
int checkedItem, DialogInterface.OnClickListener listener)则为设置单选框的方法

两者的实现均差不多,则不必赘述。

而也可以在确定按钮的时候进行各种操作

下图为运行结果



选择选项之后的结果如下:

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