android点击出现多选框,android学习笔记之弹出多选框

a4c26d1e5885305701be709a3d33442f.png

JAVA代码:

package com.meiguihuayu;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ListView;

import android.widget.Toast;

public class meiguihuayu extends Activity {

private String[] areas = new

String[]{"全部","我爱你们", "你爱我们", "他爱我们", "他们爱我", "你们爱我", "什么是爱"

};

private boolean[] areaState=new boolean[]{true,

false, false, false, false, false,false };

private ListView areaCheckListView;

private

Button Button;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main); Button=(Button)findViewById(R.id.Button);

Button.setOnClickListener(new CheckBoxClickListener());

}

class AlertClickListener implements OnClickListener{

@Override

public void onClick(View v)

{

new

AlertDialog.Builder(meiguihuayu.this).setTitle("选择区域").setItems(areas,new

DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog, int which){

Toast.makeText(meiguihuayu.this, "您已经选择了: " + which + ":" +

areas[which],Toast.LENGTH_LONG).show();

dialog.dismiss();

}

}).show();

}

}

class CheckBoxClickListener implements OnClickListener{

@Override

public void onClick(View v)

{

AlertDialog ad = new AlertDialog.Builder(meiguihuayu.this)

.setTitle("选择区域")

.setMultiChoiceItems(areas,areaState,new

DialogInterface.OnMultiChoiceClickListener(){

public void onClick(DialogInterface dialog,int whichButton, boolean

isChecked){

//点击某个区域

}

}).setPositiveButton("确定",new

DialogInterface.OnClickListener(){

public void onClick(DialogInterface dialog,int whichButton){

String s = "您选择了:";

for (int i = 0; i < areas.length; i++){

if (areaCheckListView.getCheckedItemPositions().get(i)){

s += i + ":"+ areaCheckListView.getAdapter().getItem(i)+

" ";

}else{

areaCheckListView.getCheckedItemPositions().get(i,false);

}

}

if (areaCheckListView.getCheckedItemPositions().size()

> 0){

Toast.makeText(meiguihuayu.this, s, Toast.LENGTH_LONG).show();

}else{

//没有选择

}

dialog.dismiss();

}

}).setNegativeButton("取消", null).create();

areaCheckListView = ad.getListView();

ad.show();

}

}

}

XML代码:

encoding="utf-8"?>

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:id="@+id/Button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="多选"

/>

你可能感兴趣的:(android点击出现多选框)