当前位置: 移动技术网 > 移动技术>移动开发>Android > Android开发中CheckBox的简单用法示例

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了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程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网