Mars视频S01E10_Radio单选按钮RadioButton作业

Mars视频S01E10_Radio单选RadioButton作业

实现功能:当选中第一组按钮中的maleButton的时候,会自动选中handsomeButton,当选中femaleButton的时候,会自动选中beautifulButton。
程序运行:
S01E10_Radio程序运行情况

后台情况:
S01E10_Radio后台情况

在本集视频的最后,Mars留下作业,要求两组单选按钮,当第一组里面的一个按钮被选中后,触发第二组里面的一个指定按钮会自动选中,要实现这个功能,视频中讲了监听RadioGroup中选中按钮的Id并且做出相应操作,那么我们只需要以同样的方法获取第一组里面被选中的按钮的Id,比对后,讲第二组中的指定按钮设置为选中就可以了,通过查找可以知道将RadioButton设置为选中的方法是setChecked(true)。

布局文件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:orientation="vertical"
    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.marschen.s01_e10_radio.MainActivity" >

    <RadioGroup 
        android:id="@+id/radioGroupId01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton 
            android:id="@+id/femaleButtonId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="female"
            />
        <RadioButton 
            android:id="@+id/maleButtonId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="male"
            />
    </RadioGroup>
    <RadioGroup 
        android:id="@+id/radioGroupId02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton 
            android:id="@+id/beautifulButtonId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="beautiful"
            />
        <RadioButton 
            android:id="@+id/handsomeButtonId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="handsome"
            />
    </RadioGroup>

</LinearLayout>

代码文件MainActivity.java:

package com.marschen.s01_e10_radio;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {

    private RadioGroup radioGroup01;
    private RadioGroup radioGroup02;
    private RadioButton femaleButton;
    private RadioButton maleButton;
    private RadioButton beautifulButton;
    private RadioButton handsomeButton;


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

        radioGroup01 = (RadioGroup)findViewById(R.id.radioGroupId01);
        radioGroup02 = (RadioGroup)findViewById(R.id.radioGroupId02);
        femaleButton = (RadioButton)findViewById(R.id.femaleButtonId);
        maleButton = (RadioButton)findViewById(R.id.maleButtonId);
        beautifulButton = (RadioButton)findViewById(R.id.beautifulButtonId);
        handsomeButton = (RadioButton)findViewById(R.id.handsomeButtonId);




        RadioGroupListener listener = new RadioGroupListener();
        radioGroup01.setOnCheckedChangeListener(listener);

/*      RadioButtonListener radioButtonListener = new RadioButtonListener();
        femaleButton.setOnCheckedChangeListener(radioButtonListener);*/
    }

/*  class RadioButtonListener implements android.widget.CompoundButton.OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            System.out.println("isChecked--->" + isChecked);
        }

    }*/

    class RadioGroupListener implements OnCheckedChangeListener{

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            if(checkedId == femaleButton.getId()){
                System.out.println("选中了female");
                beautifulButton.setChecked(true);
                System.out.println("选中了beautiful");
            }
            else if(checkedId == maleButton.getId()){
                System.out.println("选中了male");
                handsomeButton.setChecked(true);
                System.out.println("选中了handsome");
            }
        }

    }

    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

2条评论

评论已关闭。