java marschen_Mars视频S01E10_Radio单选按钮RadioButton作业

Mars视频S01E10_Radio单选RadioButton作业

实现功能:当选中第一组按钮中的maleButton的时候,会自动选中handsomeButton,当选中femaleButton的时候,会自动选中beautifulButton。

程序运行:

1afe605cd453b4d755633264fd69100c.png

后台情况:

ffa85a61d913934c892ff86accc3a7ac.png

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

布局文件activity_main.xml:

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" >

android:id="@+id/radioGroupId01"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/femaleButtonId"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="female"

/>

android:id="@+id/maleButtonId"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="male"

/>

android:id="@+id/radioGroupId02"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:id="@+id/beautifulButtonId"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="beautiful"

/>

android:id="@+id/handsomeButtonId"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="handsome"

/>

代码文件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);

}

}

Mars视频S01E10_Radio单选按钮RadioButton作业

更多精彩,敬请关注本博微信公众号:hsu1943

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值