Android常见控件(二)
Activity07
package org.wp.activity; import android.app.Activity; import android.os.Bundle; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class Activity07 extends Activity { private RadioGroup genderGroup; private RadioButton maleButton; private RadioButton femaleButton; private CheckBox swimBox; private CheckBox runBox; private CheckBox readBox; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); genderGroup = (RadioGroup) findViewById(R.id.gender); maleButton = (RadioButton) findViewById(R.id.maleButton); femaleButton = (RadioButton) findViewById(R.id.femaleButton); genderGroup .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { if (maleButton.getId() == checkedId) { Toast.makeText(Activity07.this, "male", Toast.LENGTH_SHORT).show(); } else if (femaleButton.getId() == checkedId) { Toast.makeText(Activity07.this, "female", Toast.LENGTH_SHORT).show(); } } }); swimBox = (CheckBox) findViewById(R.id.swim); runBox = (CheckBox) findViewById(R.id.run); readBox = (CheckBox) findViewById(R.id.read); swimBox .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { Toast.makeText(Activity07.this, "swim is checked", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(Activity07.this, "swim is unchecked", Toast.LENGTH_SHORT) .show(); } } }); runBox .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { Toast.makeText(Activity07.this, "run is checked", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(Activity07.this, "run is unchecked", Toast.LENGTH_SHORT).show(); } } }); readBox .setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { Toast.makeText(Activity07.this, "read is checked", Toast.LENGTH_SHORT).show(); } else { Toast.makeText(Activity07.this, "read is unchecked", Toast.LENGTH_SHORT) .show(); } } }); } }
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="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <RadioGroup android:id="@+id/gender" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/maleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/male" /> <RadioButton android:id="@+id/femaleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/female" /> </RadioGroup> <CheckBox android:id="@+id/swim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/swim" /> <CheckBox android:id="@+id/run" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/run" /> <CheckBox android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/read" /> </LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, Activity07!</string> <string name="app_name">Activity07</string> <string name="male">男生</string> <string name="female">女生</string> <string name="swim">游泳</string> <string name="run">跑步</string> <string name="read">读书</string> </resources>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="org.wp.activity" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Activity07" 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> <uses-sdk android:minSdkVersion="4" /> </manifest>