Android开发中CheckBox的简单用法示例

 更新时间:2016年07月06日 10:51:50   作者:无忧一生  
这篇文章主要介绍了Android开发中CheckBox的简单用法,结合实例形式分析了Android中CheckBox控件的基本功能设置与页面布局技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Android开发中CheckBox的简单用法。分享给大家供大家参考,具体如下:

CheckBox是一种在界面开发中比较常见的控件,Android中UI开发也有CheckBox,简单的说下它的使用,每个CheckBox都要设置监听,设置的监听为CompouButton.OnCheckedChangedListener()。

package com.zhuguangwei;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
public class ChkBoxActivity extends Activity {
  private TextView myTextView;
  private CheckBox myApple;
  private CheckBox myOrange;
  private CheckBox myBanana;
  private CheckBox myWaterMelon;
  private CheckBox myStrawBerry;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //通过ID找到TextView
    myTextView = (TextView) findViewById(R.id.myTextView);
    //通过ID找到这几个CheckBox
    myApple = (CheckBox) findViewById(R.id.Apple);
    myOrange = (CheckBox) findViewById(R.id.Orange);
    myBanana = (CheckBox) findViewById(R.id.banana);
    myWaterMelon = (CheckBox) findViewById(R.id.watermelon);
    myStrawBerry = (CheckBox) findViewById(R.id.strawberry);
    myApple.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        if(isChecked){
          myTextView.append(myApple.getText().toString());
        }
        else{
          if(myTextView.getText().toString().contains("苹果")){
            myTextView.setText(myTextView.getText().toString().replace("苹果", ""));
          }
        }
      }
    });
    myOrange.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        if(isChecked){
          myTextView.append(myOrange.getText().toString());
        }
        else{
          if(myTextView.getText().toString().contains("橘子")){
            myTextView.setText(myTextView.getText().toString().replace("橘子", ""));
          }
        }
      }
    });
    myBanana.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        if(isChecked){
          myTextView.append(myBanana.getText().toString());
        }
        else{
          if(myTextView.getText().toString().contains("香蕉")){
            myTextView.setText(myTextView.getText().toString().replace("香蕉", ""));
          }
        }
      }
    });
    myWaterMelon.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        if(isChecked){
          myTextView.append(myWaterMelon.getText().toString());
        }
        else{
          if(myTextView.getText().toString().contains("西瓜")){
            myTextView.setText(myTextView.getText().toString().replace("西瓜", ""));
          }
        }
      }
    });
    myStrawBerry.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      @Override
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // TODO Auto-generated method stub
        if(isChecked){
          myTextView.append(myStrawBerry.getText().toString());
        }
        else{
          if(myTextView.getText().toString().contains("草莓")){
            myTextView.setText(myTextView.getText().toString().replace("草莓", ""));
          }
        }
      }
    });
  }
}

main.xml文件内容为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <TextView
    android:id="@+id/myTextView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="请选择你一下你喜欢吃的水果:"
  />
  <CheckBox
    android:id="@+id/Apple"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="苹果"
  />
  <CheckBox
    android:id="@+id/Orange"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="橘子"
  />
  <CheckBox
    android:id="@+id/banana"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="香蕉"
  />
  <CheckBox
    android:id="@+id/watermelon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="西瓜"
  />
  <CheckBox
    android:id="@+id/strawberry"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="草莓"
  />
</LinearLayout>

运行结果为:

更多关于Android相关内容感兴趣的读者可查看本站专题:《Android控件用法总结》、《Android编程之activity操作技巧总结》、《Android视图View技巧总结》、《Android操作SQLite数据库技巧总结》、《Android操作json格式数据技巧总结》、《Android数据库操作技巧总结》、《Android文件操作技巧汇总》、《Android编程开发之SD卡操作方法汇总》、《Android资源操作技巧汇总》及《Android开发入门与进阶教程

希望本文所述对大家Android程序设计有所帮助。

相关文章

  • Android webview拦截H5的接口请求并返回处理好的数据代码示例

    Android webview拦截H5的接口请求并返回处理好的数据代码示例

    这篇文章主要给大家介绍了关于Android webview拦截H5的接口请求并返回处理好的数据的相关资料,通过WebView的shouldInterceptRequest方法,Android可以拦截并处理WebView中的H5网络请求,需要的朋友可以参考下
    2024-10-10
  • Android自定义ListView实现下拉刷新上拉加载更多

    Android自定义ListView实现下拉刷新上拉加载更多

    Listview现在用的很少了,基本都是使用Recycleview,但是不得不说Listview具有划时代的意义,我们可以自己添加下拉刷新,上拉加载更多功能。本文就来利用自定义ListView实现下拉刷新上拉加载更多效果,需要的可以参考一下
    2022-10-10
  • Android仿微信5实现滑动导航条

    Android仿微信5实现滑动导航条

    这篇文章主要为大家详细介绍了Android仿微信5实现滑动导航条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-08-08
  • 使用Android WebSocket实现即时通讯功能

    使用Android WebSocket实现即时通讯功能

    即时通讯(Instant Messaging)最重要的毫无疑问就是即时,不能有明显的延迟,要实现IM的功能其实并不难,目前有很多第三方,比如极光的JMessage,都比较容易实现。本文通过实例代码给大家分享Android WebSocket实现即时通讯功能,一起看看吧
    2019-10-10
  • Android 获取内外SD卡路径几种方法总结

    Android 获取内外SD卡路径几种方法总结

    这篇文章主要介绍了Android 获得内外SD卡路径几种方法总结的相关资料,需要的朋友可以参考下
    2016-12-12
  • 超实用的android自定义log日志输出工具类

    超实用的android自定义log日志输出工具类

    这篇文章主要为大家详细介绍了一个超实用的android自定义log日志输出工具类,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-01-01
  • Android Fragment的具体使用方式详解

    Android Fragment的具体使用方式详解

    Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕的平板电脑, 当然现在他仍然是平板APP UI设计的宠儿,而且我们普通手机开发也会加入这个Fragment, 我们可以把他看成一个小型的Activity,又称Activity片段
    2022-12-12
  • Material Design系列之Behavior实现支付密码弹窗和商品属性选择效果

    Material Design系列之Behavior实现支付密码弹窗和商品属性选择效果

    这篇文章主要为大家详细介绍了Material Design系列之Behavior实现支付密码弹窗和商品属性选择效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-09-09
  • Android开发笔记 TableLayout常用的属性介绍

    Android开发笔记 TableLayout常用的属性介绍

    今天看了安卓简单控件的布局方式,大概有绝对、相对、表格、线性、帧式布局五种方式,表格布局里面的一些属性相对来说比较复杂,下面主要谈谈表格方式布局的一些属性
    2012-11-11
  • 微信举报解除和微信解除限制的6个方法

    微信举报解除和微信解除限制的6个方法

    本文主要介绍微信被举报怎么解除?微信解除限制,这里整理了6种方法,有需要的小伙伴可以参考下
    2016-09-09

最新评论