Android checkbox和radiobutton 以及Toast和AlertDialog的使用
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的使用的更多相关文章
- [安卓] 4、CheckBox、RadioButton和Toast简单用法
和按钮类似,这里采用cb1.setOnCheckedChangeListener(this);方法分别对3个CheckBox进行CheckChange事件绑定,然后在onCheckedChange ...
- Android开发2:事件处理及实现简单的对话框(Toast,AlertDialog,Snackbar,TextInputLayout的使用)
前言 啦啦啦~又要和大家一起学习Android开发啦,博主心里好激动哒~ 在上篇博文中,我们通过线性布局和基础组件的使用,完成了一个简单的学生课外体育积分电子认证系统的界面,本篇博文,将和大家一起熟悉 ...
- 【读书笔记《Android游戏编程之从零开始》】4.Android 游戏开发常用的系统控件(EditText、CheckBox、Radiobutton)
3.4 EditText EditText类官方文档地址:http://developer.android.com/reference/android/widget/EditText.html Edi ...
- 如何在Android的ListView中构建CheckBox和RadioButton列表(支持单选和多选的投票项目示例)
引言 我们在android的APP开发中有时候会碰到提供一个选项列表供用户选择的需求,如在投票类型的项目中,我们提供一些主题给用户选择,每个主题有若干选项,用户对这些主题的选项进行选择,然后提交. 本 ...
- Android零基础入门第20节:CheckBox和RadioButton使用大全
原文:Android零基础入门第20节:CheckBox和RadioButton使用大全 本期先来学习Button的两个子控件,无论是单选还是复选,在实际开发中都是使用的较多的控件,相信通过本期的学习 ...
- android CheckBox RadioButton 照片和文字的间距问题
利用自身的定义CheckBox 要么RadioButton时间.定义自己的图标和文字在不同的手机显示不同的音高.有时不太好控制,下面是我自己的定义CheckBox: 在Layout在下面xml: &l ...
- Android UI系列-----CheckBox和RadioButton(1)
主要记录一下CheckBox多选框和RadioGroup.RadioButton单选框的设置以及注册监听器 1.CheckBox 布局文件: <LinearLayout xmlns:androi ...
- android中的 Toast 和 AlertDialog
Toast 一般用来显示一些不需要用户操作的提示信息,举个栗子: public void toast(String msg) { //---创建并设置显示的内容和显示时长 Toast toast = ...
- 设置ToggleButton、Switch、CheckBox和RadioButton的显示效果
ToggleButton.Switch.CheckBox和RadioButton都是继承自android.widget.CompoundButton,意思是可选择的,因此它们的用法都很类似.Compo ...
随机推荐
- java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory
Myeclipse 8.6使用tomcat7时间.执行javaweb如报告了以下错误项: java.lang.NoClassDefFoundError: org/apache/juli/logging ...
- swift学习笔记(六)析关闭过程和使用分配给属性的默认值
一.通过关闭和功能的默认实现财产值 当存储属性默认值需要定制,能为客户提供通过关闭或全局函数的自定义默认值. 注意:全局函数的结构,和枚举使用keywordstatic大喊 用classkeyw ...
- php学习笔记--error
不同的错误处理方法: 简单的die()语句 自己定义错误函数和错误触发器 错误报告 主要的错误处理:使用die()函数 if(!file_exists("welcome.txt") ...
- ACE定时器
每一秒钟打印一行 http://www.tuicool.com/articles/Zb263e 计时器的打开和关闭封装 http://andylin02.iteye.com/blog/440572 自 ...
- log4j 日志大小限制 分成30一个 不按日期分日志 按大小分成 按生产日期
首先说说生成按日期.不解释,大家都懂的,这种方法的缺点是很吃硬盘空间 log4j.rootLogger=INFO,logfile,stdout log4j.logger.java.sql=DEBUG, ...
- java单元测试(Junit)
JUnit是由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework),供Java开发人员编写单元测试之用. 对不同性质的被 ...
- 在C#环境中动态调用IronPython脚本(一)
本文讲述用C#调用Ironpython运行环境,解析并运行动态pyhton脚本.这种情况应用在那些需要滞后规定行为的场合,例如,动态计算项(计算引擎),用户可以自定义计算内容.计算公式等. 本文的代码 ...
- Linux C编程语言学习材料
C语言作为最基础的编程语言,30年虚弱的患病率. 无论是准备做 PHP/Java/Python/Golang 开发学习.C语言都是基础的,我们非常多基础非常小的互联网执行的开源软件服务都是C语言构筑, ...
- myeclipse如何恢复已删除的文件和代码
这是一篇文章分享秘诀:myeclipse恢复意外删除的文件和代码 [ 恢复误删文件 ] 今天在写代码的时候,不小心把一个包给删除了,然后这个包下全部的文件都没了,相信非常多人都有类似的经历. 幸好my ...
- poj 3662 Telephone Lines spfa算法灵活运用
意甲冠军: 到n节点无向图,它要求从一个线1至n路径.你可以让他们在k无条,的最大值.如今要求花费的最小值. 思路: 这道题能够首先想到二分枚举路径上的最大值,我认为用spfa更简洁一些.spfa的本 ...