手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android CheckBox 的使用案例分析
Android CheckBox 的使用案例分析
摘要:复制代码代码如下:publicclassMainActivityextendsActivity{TextViewtv;CheckBoxcb1...

复制代码 代码如下:

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 CheckBox 的使用案例分析】相关文章:

Android自定义属性 format的深入解析

Android 自定义View的使用介绍

Android 使用新浪微博SSO授权

android生命周期深入分析(一)

Android 游戏引擎libgdx 资源加载进度百分比显示案例分析

Android开发笔记 Handler使用总结

android 的 Spinner 下拉菜单的使用

android之自定义Toast使用方法

Android开发之软键盘用法实例分析

Android 开机广播的使用及配置

精品推荐
分类导航