AlertDialog和AlertDialog.builder(二)

来源:互联网 发布:java培训四个月被骗 编辑:程序博客网 时间:2024/05/07 23:35

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() {@Overridepublic void onClick(View v) {new AlertDialog.Builder(MainActivity.this).setTitle("请选择您喜欢的品牌").setIcon(R.drawable.dialog)// 这里使用setItems(items, listener)方法.setItems(tel, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog,int which) {showPhone.setText("您喜欢的手机品牌是:" + tel[which]);}}).show();}});

需要说明的是,这里使用的是AlertDialog.builder里面的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() {@Overridepublic void onClick(View v) {new AlertDialog.Builder(MainActivity.this).setTitle("请选择您喜欢的颜色").setIcon(R.drawable.dialog).setItems(R.array.color,new DialogInterface.OnClickListener() {@Overridepublic 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() {@Overridepublic void onClick(View arg0) {new AlertDialog.Builder(MainActivity.this).setTitle("请选择书籍").setIcon(R.drawable.dialog)// 复选框.setMultiChoiceItems(// 列表数组books,// 是否选中booksChoice,new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic 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() {@Overridepublic void onClick(DialogInterface dialog, int which) {}}).show();}});

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

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

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

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

下图为运行结果



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




原创粉丝点击