package com.example.radiobutton_01;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast; public class MyActivity extends Activity {
private RadioGroup rg;
private Button btn;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); rg = (RadioGroup)findViewById(R.id.rg);
btn = (Button)findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int len = rg.getChildCount();
String msg = "";
for(int i=0;i<len;i++) {
RadioButton rb = (RadioButton)rg.getChildAt(i);
if(rb.isChecked()) {
msg = rb.getText().toString();
break;
}
} Toast.makeText(MyActivity.this,msg,Toast.LENGTH_SHORT).show();
}
});
}
}
package com.example.checkbox_01;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout; import java.util.ArrayList;
import java.util.List; public class MyActivity extends Activity implements View.OnClickListener{
private List<CheckBox> checkBoxes = new ArrayList<CheckBox>();
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main); String[] checkBoxText = new String[]{
"are you student? ","are you love android?","are you dev?"
}; LinearLayout linearLayout = (LinearLayout)getLayoutInflater().inflate(R.layout.main,null); for(int i=0;i<checkBoxText.length;i++) {
CheckBox checkBox = (CheckBox)getLayoutInflater().inflate(R.layout.checkbox,null);
checkBox.setText(checkBoxText[i]); checkBoxes.add(checkBox);
linearLayout.addView(checkBox,i);
} setContentView(linearLayout); Button btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(this);
} @Override
public void onClick(View view) {
String s = "";
for(CheckBox cb : checkBoxes) {
if(cb.isChecked()) {
s+=cb.getText() + "\n";
}
} if("".equals(s)) {
s = "你没有选择选项";
} new AlertDialog.Builder(this).setMessage(s).setPositiveButton("关闭",null).show();
}
}

Android checkbox和radiobutton 以及Toast和AlertDialog的使用的更多相关文章

  1. [安卓] 4、CheckBox、RadioButton和Toast简单用法

      和按钮类似,这里采用cb1.setOnCheckedChangeListener(this);方法分别对3个CheckBox进行CheckChange事件绑定,然后在onCheckedChange ...

  2. Android开发2:事件处理及实现简单的对话框(Toast,AlertDialog,Snackbar,TextInputLayout的使用)

    前言 啦啦啦~又要和大家一起学习Android开发啦,博主心里好激动哒~ 在上篇博文中,我们通过线性布局和基础组件的使用,完成了一个简单的学生课外体育积分电子认证系统的界面,本篇博文,将和大家一起熟悉 ...

  3. 【读书笔记《Android游戏编程之从零开始》】4.Android 游戏开发常用的系统控件(EditText、CheckBox、Radiobutton)

    3.4 EditText EditText类官方文档地址:http://developer.android.com/reference/android/widget/EditText.html Edi ...

  4. 如何在Android的ListView中构建CheckBox和RadioButton列表(支持单选和多选的投票项目示例)

    引言 我们在android的APP开发中有时候会碰到提供一个选项列表供用户选择的需求,如在投票类型的项目中,我们提供一些主题给用户选择,每个主题有若干选项,用户对这些主题的选项进行选择,然后提交. 本 ...

  5. Android零基础入门第20节:CheckBox和RadioButton使用大全

    原文:Android零基础入门第20节:CheckBox和RadioButton使用大全 本期先来学习Button的两个子控件,无论是单选还是复选,在实际开发中都是使用的较多的控件,相信通过本期的学习 ...

  6. android CheckBox RadioButton 照片和文字的间距问题

    利用自身的定义CheckBox 要么RadioButton时间.定义自己的图标和文字在不同的手机显示不同的音高.有时不太好控制,下面是我自己的定义CheckBox: 在Layout在下面xml: &l ...

  7. Android UI系列-----CheckBox和RadioButton(1)

    主要记录一下CheckBox多选框和RadioGroup.RadioButton单选框的设置以及注册监听器 1.CheckBox 布局文件: <LinearLayout xmlns:androi ...

  8. android中的 Toast 和 AlertDialog

    Toast 一般用来显示一些不需要用户操作的提示信息,举个栗子: public void toast(String msg) { //---创建并设置显示的内容和显示时长 Toast toast = ...

  9. 设置ToggleButton、Switch、CheckBox和RadioButton的显示效果

    ToggleButton.Switch.CheckBox和RadioButton都是继承自android.widget.CompoundButton,意思是可选择的,因此它们的用法都很类似.Compo ...

随机推荐

  1. 基于.net开发chrome核心浏览器【二】

    原文:基于.net开发chrome核心浏览器[二] 一: 上一篇的链接: 基于.net开发chrome核心浏览器[一] 二: 相关资源介绍: chrome Frame: 让IE有一颗chrome的心, ...

  2. CentOS 6.5安全加固及性能优化

    (文章来自:http://www.cnblogs.com/seasonzone/p/3526296.html) 我们可以通过调整系统参数来提高系统内存.CPU.内核资源的占用,通过禁用不必要的服务.端 ...

  3. linux下安装cmake和mysql遇到的问题总结

    首先是在安装cmake的过程中遇到的问题: 1.開始使用yum命令安装时,不知道为什么一直不行,然后就准备wget 来先下载压缩包,再手动编译. 因为网络限制,wget不能下载外网的东西一直显示con ...

  4. Sql SUBSTR函数

    SUBSTR( ) 从一个字符表达式或备注字段中返回一个字符串.该字符串起始于字符表达式或备注字段的指定位置,到指定数目字符结束. 例如: SUBSTR(“A1”, 1 1]) 结果:A

  5. 黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block 企业库日志应用程序模块工作原理图:   从上图我们可以 ...

  6. Windows Phone开发(40):漫谈关键帧动画之中篇

    原文:Windows Phone开发(40):漫谈关键帧动画之中篇 一.DiscreteDoubleKeyFrame 离散型关键帧动画,重点,我们理解一下"离散"的意思,其实你查一 ...

  7. Android开发中验证码的生成

    近期在做电商金融类的项目,验证码的生成方法不可缺少.先学习了一种.经过測试好用.从别处学习的代码,稍修改了一下可选择是否支持识别大写和小写.直接上代码. import android.app.Acti ...

  8. Android Tombstone/Crash的log分析和定位

    有一句话叫做常在河边走,哪有不湿鞋.我们这些研究和开发Android的project师正应了这句话,相必大家在调试的时候常常会遇到这么个东西吧 *** *** *** *** *** *** *** ...

  9. ASP.NET回车提交事务

    浅析ASP.NET回车提交事件[转] ASP.NET回车提交事件其实说到底并不是ASP.NET 的编程问题,却是关于html form 中的submit 按钮就是如何规划的具体讨论. 也可归于ASP. ...

  10. Lua学习 1) —— Android呼叫变量值和分配

    2014-07-08 Lua脚本语言,嵌入在App中扩展开发是非常不错的. 关于Android与Lua的环境搭配,我直接下载别人编好的.so与.jar(放到libs下就好了) 以下简介一下Androi ...