Activity常用控件——09

来源:互联网 发布:sql do while的用法 编辑:程序博客网 时间:2024/04/30 23:12

RadioGroup

用于创建一组选中状态相互排斥的单选按钮组。RadioGroup和 RadioButton通常是放在一起使用的


RadioButton

单选按钮是一种双状态的按钮,可以选择或不选中。


CheckBox

CheckBox是有双状态按钮的复选框,可以选中也可 以不选中。

Toast

快速的返回少量信息,浮于程序上,不取得焦点


import java.util.ArrayList;    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;  import android.widget.RadioGroup.OnCheckedChangeListener;  import android.widget.TextView;    public class RadioTest extends Activity {      private ArrayList<String> sportList = new ArrayList<String>();      private TextView textView;      private RadioGroup genderGroup=null;      private RadioButton maleButton=null;      private RadioButton femaleButton=null;      private CheckBox runBox=null;      private CheckBox swimBox=null;      private CheckBox readBox=null;              /** Called when the activity is first created. */      @Override      public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.activity_main);          textView = (TextView)findViewById(R.id.textView);          genderGroup = (RadioGroup)findViewById(R.id.genderGroup);          maleButton = (RadioButton)findViewById(R.id.maleButton);          femaleButton = (RadioButton)findViewById(R.id.femaleButton);          run = (CheckBox)findViewById(R.id.run);          swim = (CheckBox)findViewById(R.id.swim);          read = (CheckBox)findViewById(R.id.read);                    run.setOnCheckedChangeListener(new OnChecked());          swim.setOnCheckedChangeListener(new OnChecked());          read.setOnCheckedChangeListener(new OnChecked());          genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {                            public void onCheckedChanged(RadioGroup group, int checkedId) {                  // TODO Auto-generated method stub                  if(femaleButton.getId() == checkedId){                      gender = "female";                  }                  else if(maleButton.getId() == checkedId){                      gender = "male";                  }              }          });                                    }      class OnChecked implements CompoundButton.OnCheckedChangeListener{          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {              // TODO Auto-generated method stub              String str = "";                            if(buttonView == run){                  str = "runBox";              }              else if(buttonView == swim){                  str = "swimBox";                 }                            else if(buttonView == read){                  str = "readBox";              }                                if(isChecked){                  if(!sportList.contains(str)){                      sportList.add(str);                  }                                }                            else if(!isChecked){                  if(sportList.contains(str)){                      sportList.remove(str);                  }                                }              String text ="gender: " + gender + "   sport: ";              for(String s : sportList){                  text += s + "  ";              }                            textView.setText(text);              Toast.makeText(RadioTest.this, text, Toast.LENGTH_LONG).show();          }                }    }  

配置文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.activity_06.RadioTest" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <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        android:id="@+id/maleButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/male"        />     </RadioGroup>     <CheckBox          android:id="@+id/swimBox"         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/swimBox"                  />     <CheckBox          android:id="@+id/readBox"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="@string/readBox"         />      <CheckBox          android:id="@+id/runBox"         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/runBox"                  /></LinearLayout>


0 0