Mars视频S01E10_Radio单选RadioButton作业
实现功能:当选中第一组按钮中的maleButton的时候,会自动选中handsomeButton,当选中femaleButton的时候,会自动选中beautifulButton。
程序运行:
在本集视频的最后,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);
}
}
标题:Mars视频S01E10_Radio单选按钮RadioButton作业
原文链接:https://beltxman.com/1094.html
若无特殊说明本站内容为 行星带 原创,未经同意请勿转载。
博主不介意我转载一下文章吧
保留链接是好人~