CompoundButton之RadioButton、CheckBox、ToggleButton、Switch

   当这四个选择式按钮的状态改变的时候,获取该按钮状态的文字,并且显示在TextView上。
   string的代码:
<string name="app_name">Itsxwz</string>
<string name="checked">选定了CB</string>
<string name="unchecked">取消了CB</string>
<string name="textOn_tbVibrate">TB是打开的</string>
<string name="textOff_tbVibrate">TB是关闭的</string>
<string name="textOn_swWifi">打开了</string>
<string name="textOff_swWifi">关闭了</string> 

   activity_main.xml的代码:
 <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"
    android:orientation="vertical"
    tools:context="com.example.itsxwz.MainActivity">

   <RadioGroup
       android:id="@+id/rgGender"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="horizontal">

      <RadioButton
          android:id="@+id/rbFemale"
          android:checked="true"
          android:text="Female"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />

      <RadioButton
          android:id="@+id/rbMale"
          android:text="Male"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" />

   </RadioGroup>

   <CheckBox
       android:id="@+id/cbPlace"
       android:layout_marginTop="10dp"
       android:text="勾选框"
       android:onClick="onPlaceClick"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

   <ToggleButton
       android:id="@+id/tbVibrate"
       android:layout_marginTop="10dp"
       android:onClick="onVibrateClick"
       android:textOn="@string/textOn_tbVibrate"
       android:textOff="@string/textOff_tbVibrate"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

   <Switch
       android:id="@+id/swWifi"
       android:layout_marginTop="12dp"
       android:padding="10dp"
       android:switchMinWidth="12sp"
       android:switchPadding="12sp"
       android:text="Wifi"
       android:textOff="@string/textOff_swWifi"
       android:textOn="@string/textOn_swWifi"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content" />

   <TextView
       android:id="@+id/tvMessage"
       android:padding="5dp"
       android:textSize="20sp"
       android:background="#ff0"
       android:layout_width="match_parent"
       android:layout_height="80dp" />

</LinearLayout>
    MainActivity.java的代码:
public class MainActivity extends AppCompatActivity {
    private TextView tvMessage;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViews();
    }

    private void findViews() {
        tvMessage = (TextView) findViewById(R.id.tvMessage);
        RadioGroup rgGender = (RadioGroup) findViewById(R.id.rgGender);
        Switch swWifi = (Switch) findViewById(R.id.swWifi);

        rgGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton)group.findViewById(checkedId);
                tvMessage.setText(radioButton.getText());
            }
        });

        swWifi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                Switch sw = (Switch) buttonView;
                String swName = sw.getText().toString();
                String message = "";
                if (isChecked){
                    message = swName + " " + sw.getTextOn();
                }else {
                    message = swName + " " + sw.getTextOff();
                }
                tvMessage.setText(message);
            }
        });

    }


    public void onPlaceClick(View v){
        CheckBox checkBox = (CheckBox) v;
        String checkBoxName = checkBox.getText().toString();
        String message;

        if (checkBox.isChecked())
            message = getString(R.string.checked) + " " + checkBoxName;
        else{
            message = getString(R.string.unchecked) + " " + checkBoxName;
        }
        tvMessage.setText(message);
    }

    public void onVibrateClick(View v){
        ToggleButton toggleButton = (ToggleButton) v;
        tvMessage.setText(toggleButton.getText());
    }

}

猜你喜欢

转载自blog.csdn.net/itsxwz/article/details/51538769