代码:

package com.test.dialog;import java.util.ArrayList;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;public class DialogTestActivity extends Activity {    /** Called when the activity is first created. */public static final String TAG="DialogTestActivity";int i=0;boolean b[]={true,false,false,false};Button btnAlertDialog;Button btnSingleItems;Button btnSingelChoices;Button btnMuiltiChoice;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                btnAlertDialog = (Button)findViewById(R.id.alertDialog);        btnAlertDialog.setOnClickListener(new ButtonViewClickListener());                btnSingleItems = (Button)findViewById(R.id.singleItemsAlertDialog);        btnSingleItems.setOnClickListener(new ButtonViewClickListener());                btnSingelChoices = (Button)findViewById(R.id.singleChoiceAlertDialog);        btnSingelChoices.setOnClickListener(new ButtonViewClickListener());                btnMuiltiChoice = (Button)findViewById(R.id.multiItemsAlertDialog);        btnMuiltiChoice.setOnClickListener(new ButtonViewClickListener());                //单选框的测试        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {public void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubRadioButton radioButton = (RadioButton)findViewById(checkedId);Log.d(TAG, String.valueOf(radioButton.getText().toString())+","+checkedId);}});                //多选框的测试       final ArrayList<CheckBox> list = new ArrayList<CheckBox>();        list.add((CheckBox)findViewById(R.id.cbAndroid));        list.add((CheckBox)findViewById(R.id.cbJava));        list.add((CheckBox)findViewById(R.id.cbPhp));        list.add((CheckBox)findViewById(R.id.cbWP7));                for(CheckBox box : list){        box.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {// TODO Auto-generated method stubCheckBox box = (CheckBox) buttonView;Log.d(TAG, isChecked+","+box.getText().toString());}});        }        Button button = (Button)findViewById(R.id.btnCheckBox);        button.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubfor(CheckBox box : list){if(box.isChecked()){Log.d(TAG, box.getText().toString());}}}});                    }            public class ButtonViewClickListener implements View.OnClickListener{ @Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.alertDialog:myAlertDialog();break;case R.id.singleItemsAlertDialog:mySingleItemsDialog();break;case R.id.singleChoiceAlertDialog:mySingleChoiceDialog();break;case R.id.multiItemsAlertDialog:myMultiItemsDialog();break;}}    }    /**     * 对话通知框     */    public void myAlertDialog(){    AlertDialog.Builder builder = new AlertDialog.Builder(this);    builder.setTitle("AlertDialog");    builder.setMessage("我是内容!");    builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubLog.d(TAG, ""+which);dialog.cancel();}});    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubLog.d(TAG, ""+which);dialog.cancel();}});    builder.create();    builder.show();    }        /**     * 单选列表对话框     */    public void mySingleItemsDialog(){     AlertDialog.Builder builder = new AlertDialog.Builder(this);    builder.setTitle("课程列表");    builder.setItems(R.array.singleItems, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubLog.d(TAG, ""+which);dialog.cancel();}});    builder.create().show();    }    /**     * 单选选项对话框     */    public void mySingleChoiceDialog(){        AlertDialog.Builder builder = new AlertDialog.Builder(this);    builder.setTitle("课程列表");    builder.setSingleChoiceItems(R.array.singleItems, i, new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubi=which;Log.d(TAG, ""+which);dialog.cancel();}});    builder.create().show();    }        /**     * 多选项列表对话框     */    public void myMultiItemsDialog(){        AlertDialog.Builder builder = new AlertDialog.Builder(this);    builder.setTitle("课程列表");    builder.setMultiChoiceItems(R.array.singleItems, b, new DialogInterface.OnMultiChoiceClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which, boolean isChecked) {// TODO Auto-generated method stubb[which] = isChecked;Log.d(TAG, ""+which+","+isChecked);}});    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {// TODO Auto-generated method stubdialog.cancel();}});    builder.create().show();    }    }


xml 文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />    <Button         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="通知对话框AlertDialog"        android:id="@+id/alertDialog"        />   <Button         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="单选列表AlertDialog"        android:id="@+id/singleItemsAlertDialog"        />   <Button         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="单选选择AlertDialog"        android:id="@+id/singleChoiceAlertDialog"        />   <Button         android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="多选项列表对话框AlertDialog"        android:id="@+id/multiItemsAlertDialog"        />   <LinearLayout        android:layout_width="fill_parent"       android:layout_height="2dp"       android:background="@android:color/darker_gray"       />   <TextView        android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="单选框RadioButton"       /><RadioGroup     android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="horizontal"    android:id="@+id/radioGroup"        > <RadioButton      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="java"     android:id="@+id/rbJava"     android:checked="true"          /> <RadioButton      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="C#"     android:id="@+id/rbC"     /> <RadioButton      android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="php"     android:id="@+id/rbPhp"     /></RadioGroup><LinearLayout        android:layout_width="fill_parent"       android:layout_height="2dp"       android:background="@android:color/darker_gray"       />   <TextView        android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="多选框CheckBox"       />   <LinearLayout        android:layout_width="fill_parent"       android:layout_height="wrap_content"       >      <CheckBox           android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="java"          android:id="@+id/cbJava"          android:checked="true"          />        <CheckBox           android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="android"          android:id="@+id/cbAndroid"          />         <CheckBox           android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="WP7"          android:id="@+id/cbWP7"          />        <CheckBox           android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="php"          android:id="@+id/cbPhp"          />     </LinearLayout>   <Button        android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="多选框CheckBox目前的值"       android:id="@+id/btnCheckBox"       /></LinearLayout>


调试输出的log

更多相关文章

  1. Android之数据存储-刘志远-专题视频课程
  2. 基于android的远程视频监控系统(已开放源码)
  3. 提供一些Android免费课程分享给大家
  4. android下拉菜单spinner的使用方法
  5. Android第五期 - 更新自己的apk本地与网络两种方法
  6. Android对话框图片全屏
  7. android EditText中inputType的属性列表
  8. Android优秀学习资源列表
  9. android EditText中inputType的属性列表

随机推荐

  1. ANDROID中LISTVIEW仿QQ群组向上滚动特效
  2. android中才Pull解析器生成xml文档
  3. Qt之Android开发环境的配置
  4. Android之传感器系统(Gsensor) .
  5. 应用程序签名
  6. 关于不需要添加android:debuggable属性就
  7. Android之自定义控件
  8. Android(安卓)设备上实现串口的移植
  9. Android内存监测工具DDMS->Heap,内存分析
  10. Android第一行代码踩坑qwq