<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:id="@+id/sel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"
android:text="选择公司"/>
<TextView
android:id="@+id/mysel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
<TextView
android:id="@+id/company"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
</LinearLayout>
package com.example.multiplydialog;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button button=null;
private TextView mysel=null;
private TextView company=null;
private TextView ceo=null;
private TextView file=null;
private int chNum=0;
private String[] companyList=null;
//定义我们默认多选的项,默认选择为第一项和第三项
private boolean[] defSel={false,false,false,false};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.button=(Button) super.findViewById(R.id.sel);
this.mysel=(TextView) super.findViewById(R.id.mysel);
this.company=(TextView) super.findViewById(R.id.company);
//为我们的按钮添加点击事件
this.button.setOnClickListener(new OnClickListenerImp());
this.companyList=MainActivity.this.getResources().getStringArray(R.array.company);
}
public class OnClickListenerImp implements OnClickListener{
public void onClick(View arg0) {
MainActivity.this.mysel.setText("");
//创建我们的单选对话框
Dialog dialog=new AlertDialog.Builder(MainActivity.this)
.setTitle("选择公司")
.setIcon(R.drawable.ic_launcher)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//MainActivity.this.mysel.setText("您的选择是"+companyList[chNum]);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setMultiChoiceItems(R.array.company, defSel, new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
for(int i=0;i<companyList.length;i++){
if(i==which&&isChecked){
//表示选中了
MainActivity.this.mysel.append(MainActivity.this.companyList[which]+" ");
}
}
}
})
.create();
dialog.show();
}
}
}