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. ecshop购物流程中去掉email邮箱

    首先打开includes\lib_order.php,在第1688行左右找到并删除 !empty($consignee['email']) && 接着打开js\shopping_flo ...

  2. 重写ResultSet实现分页功能(最好的分页技术)(转)

    1.首先定义一个接口Pageable 继承ResultSet这个类 并在接口中定义一些自己的方法,具体方法如下: package com.page; import java.sql.ResultSet ...

  3. Windows Phone开发(14):数据模板

    原文:Windows Phone开发(14):数据模板 数据模板,如果你仅仅听到这个名词,你一定很迷惑,什么来的?用来干什么的?不急,亲,今天,我们一起来探索一下吧. 用白话文说,数据模板就是用来规范 ...

  4. 【buildroot-2011.11】You may have to install &#39;g++&#39; on your build machine

    buildroot - 2011.11 当进行交叉编译.例如像以下错误提及演示: "You may have to install 'g++' on your build machine&q ...

  5. twisted是python实现的基于事件驱动的异步网络通信构架。

    网:https://twistedmatrix.com/trac/ http://www.cnblogs.com/wy-wangyan/p/5252271.html What is Twisted? ...

  6. js控制图片缩放、水平和垂直方向居中对齐

    已測试兼容 IE6,IE7,IE8,火狐FF,谷歌chrome. 这里使用了jquery插件,假设你不使用jquery,略微改造一下也非常快. 网上查了些资料,用css控制兼容性不好,看去非常揪心.于 ...

  7. 【转】Qt事件循环与线程 二

    转自:http://blog.csdn.net/changsheng230/article/details/6153449 续上文:http://blog.csdn.net/changsheng230 ...

  8. Codeforces 9A-Die Roll(意甲冠军)

    A. Die Roll time limit per test 1 second memory limit per test 64 megabytes input standard input out ...

  9. DOM操作应用

    创建元素 document.createElement("li"); 添加节点 oUl.appendChild(oLi); 在某个元素之前插入一个节点 oUl.insertBefo ...

  10. 返璞归真 asp.net mvc (12) - asp.net mvc 4.0 新特性之移动特性

    原文:返璞归真 asp.net mvc (12) - asp.net mvc 4.0 新特性之移动特性 [索引页][源码下载] 返璞归真 asp.net mvc (12) - asp.net mvc ...