代码:
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 stub
RadioButton 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() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
CheckBox box = (CheckBox) buttonView;
Log.d(TAG, isChecked+","+box.getText().toString());
}
});
}
Button button = (Button)findViewById(R.id.btnCheckBox);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
for(CheckBox box : list){
if(box.isChecked()){
Log.d(TAG, box.getText().toString());
}
}
}
});
}
public class ButtonViewClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (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(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.d(TAG, ""+which);
dialog.cancel();
}});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.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() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Log.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() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
i=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() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
b[which] = isChecked;
Log.d(TAG, ""+which+","+isChecked);
}
});
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.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
分享到:
相关推荐
android Dialog效果集合【全】
包括所有Android Dialog ,代码写的比较详细,希望能给大家带来帮助。
android dialog用法集合与举例
Android 对话框 dialog 集合 收集所有对话框事例
Android dialog的显示位置、弹出效果、返回键控制、外部点击事件控制
将Android中各种Dialog集合了一下,希望对新手Dialog的学习有所帮助
dialog窗口大集合,收集整理常用的dialog样式。
android对话框大集合 各种你需要的android对话框 单选,多选,列表,进度条的对话框
主要为大家分享了一个属于自己的Android对话框(Dialog)自定义集合,建立自己的Android对话框,感兴趣的小伙伴们可以参考一下
Android开发人员不得不收集的工具类集合 | 支付宝支付 | 微信支付(统一下单) | 微信分享 | 一键集成UCrop选择圆形头像 | 一键集成二维码和条形码的扫描与生成 | 常用Dialog | WebView的封装可播放视频 | 仿斗鱼...
Android与js交互.rar Android中监听电话状态.rar Android之GPS定位.zip Android之Wifi学习教程.rar Android之用PopupWindow实现弹出菜单.rar android写的google map api 应用.rar android在wifi下手机与电脑的socket...
Android开发不得不收集的工具类集合, 工欲善其事必先利其器!Android开发过程经常需要用到各式各样的工具类,虽然大部分只需谷歌/百度一下就能找到;但是有时候急需使用却苦苦搜寻不到。于是给大家介绍一个github上...
Android开发人员不得不收集的工具类集合 | 支付宝支付 | 微信支付(统一下单) | 微信分享 | Zip4j压缩(支持分卷压缩与加密) | 一键集成UCrop选择圆形头像 | 一键集成二维码和条形码的扫描与生成 | 常用Dialog | ...
Android例子源码强大的listview效果集合项目 Android例子源码模仿IOS音乐的可拖动导航栏功能 Android例子源码模仿安卓微信、云播雷达扫描动画效果 Android例子源码类似58同城的通过滑屏控制引导页 Android例子源码计...
介绍 学习Android开发的代码,基于AndroidStudio View_Demo是Android基础控件和布局,包括TextView,Edittext,ImageView,Button,RadioButton,Checkbox,ProgressBar和拖动条。 Adapter_Demo 是Adapter相关的代码...
Android开发人员不得不收集的工具类集合 | 支付宝支付 | 微信支付(统一下单) | 微信分享 | Zip4j压缩(支持分卷压缩与加密) | 一键集成UCrop选择圆形头像 | 一键集成二维码和条形码的扫描与生成 | 常用Dialog | ...
android/**进度条框 **//**多个按钮信息框 **//**列表框 **/ /**单项选择列表框 **/ /**多项选择列表框 **/ /**自定义布局 **/ /**读取进度框 **/
工具类集合 MLog工具类 日期获取工具类 网络检查工具类 权限检查工具类 MD5工具类 日志保存工具类 SheetDialog 底部弹出框 LogcatHelper.getInstance(this).start(filePath); 开启保存日志 LogcatHelper....
第三方lib集合 Android应用图标 Android系统日历 加解密 自定义View以及动画 弹幕 Android个人网站 Android日期相关 Android埋点 面试相关 Android Tablayout Flutter Android悬浮窗 Android视频 RecyclerView ...