Android基础-CheckBox用法
来源:互联网 发布:2016加入淘宝客的条件 编辑:程序博客网 时间:2024/06/05 20:54
CheckBox与前面的RadioButton相反它是一个多选按钮这里给大家边写了一个按钮希望大家可以喜欢。
我们还是说一说它的一个实现步骤:
1、在我们的布局文件中定义好我们的布局
2、在Activity中获取我们布局好的控件
3.设置单击事件的
4、通过ishecked()这么一个方法来判断我们CheckBook是否选中选中的话就想我们定义好的字符串中添加数据
5、最后进行一个显示
-----------------------------------------------------------------------------
布局文件代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="你为什么要学习Android "/>
<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="工资高"/>
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜欢3g开发"/>
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="父母要求学的"/>
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交" />
</LinearLayout>
</RelativeLayout>
----------------------------------------------------------------------
Activity中的代码:
package com.example.checkbox;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
public class MainActivity extends Activity {
//定义一个字符串用来保存数据
private String s;
private CheckBox cb1,cb2,cb3;
private Button bt;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb1=(CheckBox) findViewById(R.id.cb1);
cb2=(CheckBox) findViewById(R.id.cb2);
cb3=(CheckBox) findViewById(R.id.cb3);
bt=(Button) findViewById(R.id.bt);
//设置按钮的单击事件
bt.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
//通过isChecked()方法进行判断保存数据到s中
if (!cb1.isChecked()&&!cb2.isChecked()&&!cb3.isChecked()) {
s+="请至少现在一项!";
}
if (cb1.isChecked()) {
s+=cb1.getText()+"\n";
}
if (cb2.isChecked()) {
s+=cb2.getText()+"\n";
}
if (cb3.isChecked()) {
s+=cb3.getText()+"\n";
}
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
s="";
}
});
}
}
心得:
其实大家不要看着代码好像很多的样子其实关键的代码就只有红色的那么一点,最后s="";是对保存的一个数据进行清空的,这样我们下次点击的时候就不会连同上次的数据一起显示。
- Android基础-CheckBox用法
- android基础——CheckBox
- 【幻化万千戏红尘】qianfeng-Android-Day03-RadioButton及RadioGroup的用法、CheckBox、ProgressBar基础学习:
- android sdk---checkBox的用法实例
- Android初级之组件基础06-CheckBox
- Android 基础控件之CheckBox与RadioButton
- 0824Android基础CheckBox+ImageView+ListView
- Android基础控件 - RadioButton与CheckBox
- checkbox用法
- CHECKBOX用法
- checkbox用法
- CheckBox用法
- checkbox用法
- checkBox用法
- Android.mk用法基础
- android 数据库基础用法
- Android基础-TextView用法
- Android基础-Button用法
- Linux防火墙开放某个端口
- Database Vault注册
- #ffffffff透明度参照表
- Android设计模式之-观察着模式
- UIScrollView基本用法&代理方法
- Android基础-CheckBox用法
- window 7 IIS配置方法 win7 Internet信息服务配置方法详解
- Android注销功能
- maven: Missing artifact xxx
- 快速掌握Lua 5.3 —— Lua与C之间的交互概览
- Linux下关于cvs的迁移
- 虚函数初探
- linux 常用指令
- Windows核心编程读书笔记之ErrorLookup