您的位置:首页 > 其它

常用的组件RadioGroup和CheckBox笔记

2013-12-23 06:23 309 查看
RadioGroup和CheckBox在Android应用中相对常见,下面就把本人初次学习的笔记放上来。

Activity Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<RadioGroup
android:id="@+id/sex"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<RadioButton
android:id="@+id/man"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/man" />

<RadioButton
android:id="@+id/woman"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/woman" />
</RadioGroup>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<CheckBox
android:id="@+id/baseball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/baseball" >
</CheckBox>

<CheckBox
android:id="@+id/basketball"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/basketball" >
</CheckBox>

<CheckBox
android:id="@+id/football"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/football" >
</CheckBox>

<CheckBox
android:id="@+id/swimming"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swimming" >
</CheckBox>
</LinearLayout>

</LinearLayout>
Android Code

package cn.qiuzhping.module1;

import cn.qiuzhping.module1.R;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends Activity {

private RadioGroup sex = null;
private RadioButton man = null;
private RadioButton woman = null;
private CheckBox baseball = null;
private CheckBox basketball = null;
private CheckBox swimming = null;
private CheckBox football = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sex = (RadioGroup) findViewById(R.id.sex);
man = (RadioButton) findViewById(R.id.man);
woman = (RadioButton) findViewById(R.id.woman);
baseball = (CheckBox) findViewById(R.id.baseball);
basketball = (CheckBox) findViewById(R.id.basketball);
swimming = (CheckBox) findViewById(R.id.swimming);
football = (CheckBox) findViewById(R.id.football);
sexSelect();
hobbySelect();
}

public void hobbySelect() {

baseball.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Log.i("CheckBox", "baseball is Checked");
} else {
Log.i("CheckBox", "baseball is not Checked");
}
}

});

basketball
.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Log.i("CheckBox", "basketball is Checked");
} else {
Log.i("CheckBox", "basketball is not Checked");
}
}

});

swimming.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Log.i("CheckBox", "swimming is Checked");
} else {
Log.i("CheckBox", "swimming is not Checked");
}
}

});

football.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
Log.i("CheckBox", "football is Checked");
} else {
Log.i("CheckBox", "football is not Checked");
}
}

});

}

public void sexSelect() {

sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (man.getId() == checkedId) {
Log.i("onCheckedChanged", "选择man");
} else if (woman.getId() == checkedId) {
Log.i("onCheckedChanged", "选择woman");
} else {
Log.i("onCheckedChanged", "没有选择");
}
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.qiuzhping.module1"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="14" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="cn.qiuzhping.module1.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: