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 ...
随机推荐
- AS3.0下去除flash右键菜单
这两天工作中遇到一个问题,就是网页中内嵌的flash小游戏的用户体验,当鼠标在flash上点击右键时,出现的右键菜单中会有播放,停止等选项,虽然不会造成什么漏洞,但是体验非常差.在寻找解决方案的时候, ...
- 关于SSIS批量抽取Excel文件报0x80004005错误的解决办法
原文:关于SSIS批量抽取Excel文件报0x80004005错误的解决办法 标题: Microsoft Visual Studio ------------------------------ Pa ...
- asp.net Login控件基本属性及事件说明
原文:asp.net Login控件基本属性及事件说明 Login系列控件是微软为了简化我们的开发过程,为我们进行常规的安全开发提供块捷途径. Login系列控件包含下列控件: Login 登录控件 ...
- POJ 1002 487-3279 Trie解读
这个问题的解决方法是多种多样的.如本文所用,Trie为了解决这个问题. 它也可用于hash表.map等解决方案,由于输入是特定7数字,因此,你应该能够解决. 如本文所用,Trie不是非常快.最后,我主 ...
- 採用Android中的httpclient框架发送post请求
/** * 採用httpclientPost请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得 ...
- python基础课程_学习笔记15:标准库:有些收藏夹——fileinput
标准库:有些收藏夹 fileinput 重要功能 性能 叙述性说明 input([files[,inplace[,backup]]) 便于遍历多个输入流中的行 filename() 返回当前文件的名称 ...
- win7,win8取得管理员权限 .reg文件
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\*\shell\runas] @="获取管理员所有权" " ...
- GCD code block
在这里积累一些片段,由于备忘录. + (DRClass *)sharedDR{ //创建小黑.正常ap模式仅仅有一个小黑,so static DRClass *aDR = nil; static di ...
- LCA 学习算法 (最近的共同祖先)poj 1330
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20983 Accept ...
- Dom操作高级应用
table tBodies,tHead,tFoot,rows,cells 一个table有多个tbody oTab.tBodies[0].rows[i].style.background = &quo ...