您的位置:首页 > 移动开发 > Android开发

Android之复选框CheckBox使用

2015-09-26 10:49 741 查看
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:layout_width="wrap_content<span id="transmark"></span>" android:layout_height="wrap_content"
android:id="@+id/checkbox_test"
android:text="@string/str_checkbox_view"/>
<CheckBox android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="@+id/myCheckBox01"
android:text="@string/str_checkbox01"/>
<CheckBox android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="@+id/myCheckBox02"
android:text="@string/str_checkbox02"/>
<CheckBox android:layout_width="fill_parent" android:layout_height="wrap_content"
android:id="@+id/myCheckBox03"
android:text="@string/str_checkbox03"/>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/checkbox_view"/>
</LinearLayout>


strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">myRadio</string>
<string name="str_checkbox_view">CheckBox test: </string>
<string name="str_checkbox01">checkbox_01</string>
<string name="str_checkbox02">checkbox_02</string>
<string name="str_checkbox03">checkbox_03</string>
</resources>


MyActivity.java

package com.example.myRadio;

import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class MyActivity extends Activity {

private TextView mTextView01;
private CheckBox mCheckBox01;
private CheckBox mCheckBox02;
private CheckBox mCheckBox03;

/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//bind Element
mTextView01 = (TextView)findViewById(R.id.checkbox_view);
mCheckBox01 = (CheckBox)findViewById(R.id.myCheckBox01);
mCheckBox02 = (CheckBox)findViewById(R.id.myCheckBox02);
mCheckBox03 = (CheckBox)findViewById(R.id.myCheckBox03);

mTextView01.setText("your choose are: ");

//bind Listener
mCheckBox01.setOnCheckedChangeListener(onCheckedChangeListener);
mCheckBox02.setOnCheckedChangeListener(onCheckedChangeListener);
mCheckBox03.setOnCheckedChangeListener(onCheckedChangeListener);
}

private CheckBox.OnCheckedChangeListener onCheckedChangeListener = new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
String str0 = "the choose are:";
String str1 = getString(R.string.str_checkbox01);
String str2 = getString(R.string.str_checkbox02);
String str3 = getString(R.string.str_checkbox03);

if (mCheckBox01.isChecked()){
str0 = str0 + str1 + " ";
}
if (mCheckBox02.isChecked()){
str0 = str0 + str2 + " ";
}
if (mCheckBox03.isChecked()){
str0 = str0 + str3;
}
mTextView01.setText(str0);
}
};
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: