欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

Android CheckBox 的使用案例分析

时间:2021-03-29 09:42:30|栏目:Android代码|点击:

复制代码 代码如下:

public class MainActivity extends Activity {
 TextView tv;
 CheckBox cb1;
 CheckBox cb2;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  cb1 = (CheckBox) findViewById(R.id.checkbox1);
  cb2 = (CheckBox) findViewById(R.id.checkbox2);
  tv = (TextView) findViewById(R.id.textview1);

  cb1.setOnCheckedChangeListener(cb);
  cb2.setOnCheckedChangeListener(cb);
 }

 private CheckBox.OnCheckedChangeListener cb = new CheckBox.OnCheckedChangeListener() {
  @Override
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
   String stv = getString(R.string.hoby);
   String scb1 = getString(R.string.basketball);
   String scb2 = getString(R.string.football);
   if (cb1.isChecked() && cb2.isChecked()) {
    tv.setText(stv + ":" + scb1 + "&&" + scb2);
   } else if (cb1.isChecked() && !cb2.isChecked()) {
    tv.setText(stv + ":" + scb1);
   } else if (!cb1.isChecked() && cb2.isChecked()) {
    tv.setText(stv + ":" + scb2);
   } else {
    tv.setText(stv);
   }
  }
 };


如下是布局文件:
复制代码 代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <TextView

        android:id="@+id/textview1"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="@string/hoby" />

    <CheckBox

        android:id="@+id/checkbox1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/basketball" />

    <CheckBox

        android:id="@+id/checkbox2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:text="@string/football" />

</LinearLayout>

上一篇:Android应用开发中实现apk皮肤文件换肤的思路分析

栏    目:Android代码

下一篇:Android 自定义输入手机号自动添加分隔符

本文标题:Android CheckBox 的使用案例分析

本文地址:http://www.codeinn.net/misctech/90357.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有