Android Alertdialog之多选对话框

    Android UI各种各样,下面是自己学习时写的多选对话框的实例。也在网上看了很多实例,但很多实例都没有将选择后的值显示出来,或者选择后,当用户取消选择时,以前选择的值还在的情况很多。下面是实例不会有这样的问题。

    Android中的对话框形式有四种,分别是一般对话框形式,列表对话框形式,单选按钮对话框,多选按钮对话框,这里主要是针对多选对框框的介绍。

一、创建alertdialog工程,如图

Android Alertdialog之多选对话框

二、activity_main.xml中代码

Android Alertdialog之多选对话框
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="显示对话框" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="45dp"
        android:text="TextView" />

</RelativeLayout>
View Code

三、MainActivity中的代码

Android Alertdialog之多选对话框
package com.bruce.alertdialog;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {

    private Button button;
    private TextView textView;
    private CharSequence[] items = {"北京","上海","贵州"};
    private boolean[] mulchoice = new boolean[items.length];
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button1);
        textView = (TextView)findViewById(R.id.textView1);
        
        button.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                for(int i = 0;i<items.length;i++){
                    mulchoice[i] = false;
                }
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("请选择");
                builder.setMultiChoiceItems(items, mulchoice, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        
                        mulchoice[which] = isChecked;
                        Log.i("MyTest", "-->which=" + which);
                        
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String s = "";
                        for(int i=0;i<items.length;i++){
                            if(mulchoice[i]){
                                s = s + items[i] + ",";
                            }
                        }
                        textView.setText(s.substring(0, s.length()-1));
                    }
                });
                
                AlertDialog dialog = builder.create();
                dialog.show();
                
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
View Code

四、测试

 Android Alertdialog之多选对话框Android Alertdialog之多选对话框Android Alertdialog之多选对话框

Android Alertdialog之多选对话框

上一篇:Android学习小Demo(9)一个To Do List的实现


下一篇:unity 判断 是手机还是平板