01_12_Android常见控件(二) (RadioGroup、RadioButton、CheckBox、Toast)
1.RadioGroup和RadioButton的使用方法
2.CheckBox的使用方法
3.Toast的使用方法
-
//main.xml
-
<LinearLayout xmlns:android=""
-
android:orientation="vertical"
-
android:layout_width="fill_parent"
-
android:layout_height="fill_parent"
-
>
-
<TextView
-
android:id="@+id/textView1"
-
android:layout_width="fill_parent"
-
android:layout_height="wrap_content"
-
android:text="@string/hello"
-
/>
-
-
<RadioGroup
-
android:id="@+id/genderGroup"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:orientation="vertical"
-
>
-
<RadioButton
-
android:id="@+id/femaleButton"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="@string/female"
-
>
-
</RadioButton>
-
<RadioButton
-
android:id="@+id/maleButton"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="@string/male"
-
>
-
</RadioButton>
-
</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>
-
//RodioTest.java
-
package com.lwb.myactivity_07;
-
-
import android.os.Bundle;
-
import android.app.Activity;
-
import android.widget.CheckBox;
-
import android.widget.CompoundButton;
-
import android.widget.RadioButton;
-
import android.widget.RadioGroup;
-
import android.widget.Toast;
-
-
public class RodioTest extends Activity {
-
-
private RadioGroup genderGroup=null;
-
private RadioButton femaleButton=null;
-
private RadioButton maleButton=null;
-
-
private CheckBox swimBox=null;
-
private CheckBox runBox=null;
-
private CheckBox readBox=null;
-
-
@Override
-
protected void onCreate(Bundle savedInstanceState) {
-
super.onCreate(savedInstanceState);
-
setContentView(R.layout.main);
-
-
//通过控件的ID来得到代表空间的对象
-
genderGroup=(RadioGroup)findViewById(R.id.genderGroup);
-
femaleButton=(RadioButton)findViewById(R.id.femaleButton);
-
maleButton=(RadioButton)findViewById(R.id.maleButton);
-
-
swimBox=(CheckBox)findViewById(R.id.swim);
-
runBox=(CheckBox)findViewById(R.id.run);
-
readBox=(CheckBox)findViewById(R.id.read);
-
-
//为RadioGroup设置监听器,需要注意的是,这里的监听器和Button空间的监听器是不同的
-
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
-
-
@Override
-
public void onCheckedChanged(RadioGroup group, int checkedId) {
-
// TODO Auto-generated method stub
-
if(femaleButton.getId()==checkedId){
-
System.out.println("female");
-
//Toast.makeText(context, text, duration)
-
Toast.makeText(RodioTest.this, "female", Toast.LENGTH_SHORT).show();
-
}
-
else if(maleButton.getId()==checkedId){
-
System.out.println("male");
-
Toast.makeText(RodioTest.this,"male",Toast.LENGTH_SHORT).show();
-
}
-
}
-
});
-
-
//为多选按钮添加监听器
-
swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
-
-
@Override
-
public void onCheckedChanged(CompoundButton buttonView,
-
boolean isChecked) {
-
// TODO Auto-generated method stub
-
if(isChecked){
-
System.out.println("swim is checked");
-
}
-
else{
-
System.out.println("swim is unchecked");
-
}
-
}
-
});
-
-
runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
-
-
-
@Override
-
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-
// TODO Auto-generated method stub
-
if(isChecked){
-
System.out.println("run is checked");
-
}
-
else{
-
System.out.println("run is unchecked");
-
}
-
}
-
});
-
-
readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
-
-
-
@Override
-
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-
// TODO Auto-generated method stub
-
if(isChecked){
-
System.out.println("read is checked");
-
}
-
else{
-
System.out.println("read is unchecked");
-
}
-
}
-
});
-
-
}
-
-
/* @Override
-
public boolean onCreateOptionsMenu(Menu menu) {
-
// Inflate the menu; this adds items to the action bar if it is present.
-
getMenuInflater().inflate(R.menu.rodio_test, menu);
-
return true;
-
}*/
-
-
}
1.RadioGroup和RadioButton的使用方法
-
<RadioGroup
-
android:id="@+id/genderGroup"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:orientation="vertical"
-
>
-
<RadioButton
-
android:id="@+id/femaleButton"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="@string/female"
-
>
-
</RadioButton>
-
<RadioButton
-
android:id="@+id/maleButton"
-
android:layout_width="wrap_content"
-
android:layout_height="wrap_content"
-
android:text="@string/male"
-
>
-
</RadioButton>
-
</RadioGroup>
-
private RadioGroup genderGroup=null;
-
private RadioButton femaleButton=null;
-
private RadioButton maleButton=null;
-
-
//通过控件的ID来得到代表空间的对象
-
genderGroup=(RadioGroup)findViewById(R.id.genderGroup);
-
femaleButton=(RadioButton)findViewById(R.id.femaleButton);
-
maleButton=(RadioButton)findViewById(R.id.maleButton);
-
-
//为RadioGroup设置监听器,需要注意的是,这里的监听器和Button空间的监听器是不同的
-
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
-
-
@Override
-
public void onCheckedChanged(RadioGroup group, int checkedId) {
-
// TODO Auto-generated method stub
-
if(femaleButton.getId()==checkedId){
-
System.out.println("female");
-
//Toast.makeText(context, text, duration)
-
Toast.makeText(RodioTest.this, "female", Toast.LENGTH_SHORT).show();
-
}
-
else if(maleButton.getId()==checkedId){
-
System.out.println("male");
-
Toast.makeText(RodioTest.this,"male",Toast.LENGTH_SHORT).show();
-
}
-
}
-
});
2.CheckBox的使用方法
-
<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"
-
/>
-
private CheckBox swimBox=null;
-
private CheckBox runBox=null;
-
private CheckBox readBox=null;
-
-
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) {
-
// TODO Auto-generated method stub
-
if(isChecked){
-
System.out.println("swim is checked");
-
}
-
else{
-
System.out.println("swim is unchecked");
-
}
-
}
-
});
-
-
runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
-
-
-
@Override
-
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-
// TODO Auto-generated method stub
-
if(isChecked){
-
System.out.println("run is checked");
-
}
-
else{
-
System.out.println("run is unchecked");
-
}
-
}
-
});
-
-
readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
-
-
-
@Override
-
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
-
// TODO Auto-generated method stub
-
if(isChecked){
-
System.out.println("read is checked");
-
}
-
else{
-
System.out.println("read is unchecked");
-
}
-
}
-
});
3.Toast的使用方法
-
Toast.makeText(RodioTest.this, "female", Toast.LENGTH_SHORT).show();
这里就是几个简单控件的使用
阅读(473) | 评论(0) | 转发(0) |