Android---20---CheckBox复选框

MainActivity.java:

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
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 implements OnClickListener {
	private CheckBox basketball,football,baseball,pingpang;
	private Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		basketball = (CheckBox) findViewById(R.id.basketball);
		baseball = (CheckBox) findViewById(R.id.baseball);
		football = (CheckBox) findViewById(R.id.football);
		pingpang = (CheckBox) findViewById(R.id.pingpang);
		button = (Button) findViewById(R.id.button);
		button.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		String s = "";
		boolean flag = false;
		if (basketball.isChecked()){
			s += basketball.getText().toString();
			flag = true;
		}
		if (baseball.isChecked()){
			s += baseball.getText().toString();
			flag = true;
		}
		if (football.isChecked()){
			s += football.getText().toString();
			flag = true;
		}
		if (pingpang.isChecked()){
			s += pingpang.getText().toString();
			flag = true;
		}
		if (flag == false){
			Toast.makeText(MainActivity.this, "没有任何选项", 0).show();
		}else {
			Toast.makeText(MainActivity.this, s, 0).show();
		}
	}
}

activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    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="com.example.checkboxdemo2.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="选择爱好" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <CheckBox
            android:id="@+id/basketball"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="篮球" />

        <CheckBox
            android:id="@+id/football"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="足球" />

        <CheckBox
            android:id="@+id/baseball"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="网球" />

        <CheckBox
            android:id="@+id/pingpang"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="乒乓球" />
    </LinearLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="确定" />

</LinearLayout>
时间: 2024-08-10 16:46:49

Android---20---CheckBox复选框的相关文章

CheckBox复选框控件

CheckBox复选框控件 一.简介 1. 2.类结构图 二.CheckBox复选框控件使用方法 这里是使用java代码在LinearLayout里面添加控件 1.新建LinearLayout布局 2.建立CheckBox的XML的Layout文件 3.通过View.inflate()方法创建CheckBox CheckBox checkBox=(CheckBox) View.inflate(this, R.layout.checkbox, null); 4.通过LinearLayout的add

CheckBox复选框回显

CheckBox复选框回显,分两个部分查数据: 其一: 查询所有复选框数据,拼接成复选框 其二,根据查询实际场景id,查询复选框对应数据, 设计循环嵌套逻辑进行复选框数据回显默认选中: 1 //双击行事件 2 function onDblClickRow(rowIndex, rowData){ 3 4 //权限复选框置空 5 $('#permissionTd').html(""); 6 7 //打开修改窗口 8 $('#editWindow').window("open&qu

步步为营_Android开发课[24]_用户界面之Checkbox(复选框)

Focus on technology, enjoy life!-- QQ:804212028 浏览链接:http://blog.csdn.net/y18334702058/article/details/44624305 主题:用户界面之Checkbox(复选框) - Checkbox复选框(实例): activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <LinearLayout x

RadioButton(单选按钮)&amp;Checkbox(复选框)

本节引言: 本节给大家带来的是Andoird基本UI控件中的RadioButton和Checkbox; 先说下本节要讲解的内容是:RadioButton和Checkbox的 1.基本用法 2.事件处理: 3.自定义点击效果: 4.改变文字与选择框的相对位置: 5.修改文字与选择框的距离 其实这两个控件有很多地方都是类似的,除了单选和多选,事件处理,其他的都是类似的! 另外还有一个ListView上Checkbox的错位的问题,我们会在ListView那一章对这个问题进行 解决,好的,开始本节内容

elementUI 学习入门之 checkbox 复选框

CheckBox 复选框 与单选框基本类似.如:按钮样式.带边框.复选框按钮大小. eg: <template> <el-checkbox-group v-model="selectCities" min='2' max="3"> <el-checkbox v-for="city in cities" :label="city" :key="city">{{city}}&

如何判断checkbox复选框是否选中

如何判断checkbox复选框是否选中:本章节介绍一下如何用jquery判断一个checkbox复选框是否被选中,方式有很多种,这里只是简单介绍一下比较常用的两种方式,不提供完整的代码,给出主要的代码片段.方式一: $("#ck").prop("checked")==true 关于prop()可以参阅jQuery的prop()方法一章节.方式二: $("#ck").is(":checked") :checked可以参阅jQue

JavaScript操作checkbox复选框

JavaScript操作checkbox的方式和操作radio的方式相似,都是利用元素项的checked属性来完成.先获取checkbox元素集合,遍历集合,对集合中的每一项做操作. 这里讲几个常用的checkbox复选框的常见示例. 取值 给定页面 <body> <p> <label for="hobby">Hobby:   <input type="checkbox" name="hobby" val

纯css3实现的超炫checkbox复选框和radio单选框

之前为大家分享了好多css3实现的按钮.今天要为大家分享的是纯css3实现的checkbox复选框和radio单选框,效果超级炫.先让我们看看图吧! 在线预览   源码下载 这个实例完全由css3实现的没有任何js代码.下面我们一起看下实现代码吧 html代码: <div style="width:200px; float:left"> <label> <input type="checkbox" class="option-

点击文本框弹出可供选择的checkbox复选框代码实例

点击文本框弹出可供选择的checkbox复选框代码实例:本章节分享一段代码实例,它能够点击文本框的时候,能够弹出下拉的checkbox复选框,选中复选框就能够将值写入文本框中,可能在实际应用中的效果没有这么直白简单,不过可以作为一个例子演示,以便于学习者理解和扩展.代码如下: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="author&qu

css3的实现的checkbox复选框和radio单选框绚丽美化效果

css3的实现的checkbox复选框和radio单选框绚丽美化效果:在css3之前要美化单选框和复选框无非是使用图片进行相关的替换操作,并且还有很大的局限性.由于css3的出现,一切好像变的都变的轻松起来,并且效果非常的绚丽,这是使用css2无法做到的,下面就分享一段能够实现此功能的代码实例,需要的朋友可以做一下参考.代码如下: <!DOCTYPE html><html> <head> <meta charset=" utf-8"> &