作业
一、页面布局
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.helloworld.myapplication.MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:text=" NBA巅峰时刻 "
android:gravity="center"
android:textColor="#993399"
android:textSize="30sp" />
<ImageView
android:id="@+id/picture"
android:layout_width="250dp"
android:layout_height="200dp"
android:layout_gravity="center"
android:src="@drawable/nba"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:orientation="horizontal">
<RadioGroup
android:id="@+id/radio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingEnd="20dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/QD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="乔丹" />
<RadioButton
android:id="@+id/KB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="科比" />
<RadioButton
android:id="@+id/MD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="麦迪" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="25dp"
android:orientation="horizontal">
<RadioGroup
android:id="@+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingEnd="25dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/ZMS"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="詹姆斯"
android:textSize="25sp" />
<RadioButton
android:id="@+id/KL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="库里" />
<RadioButton
android:id="@+id/ZBL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:text="张伯伦" />
</RadioGroup>
</LinearLayout>
</RadioGroup>
二、JAVA代码及功能实现
package com.helloworld.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity {
private ImageView picture;
private RadioGroup radio;
private RadioButton KB;
private RadioButton QD;
private RadioButton MD;
private RadioGroup radio2;
private RadioButton ZMS;
private RadioButton KL;
private RadioButton ZBL;
private ImageView nba;
ImageView kb,kl,md,qd,zms,zbl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
picture = (ImageView) findViewById(R.id.picture);
radio= (RadioGroup) findViewById(R.id.radio);
KB = (RadioButton) findViewById(R.id.KB);
QD = (RadioButton) findViewById(R.id.QD);
MD = (RadioButton) findViewById(R.id.MD);
radio2=(RadioGroup) findViewById(R.id.radio2) ;
ZMS = (RadioButton) findViewById(R.id.ZMS);
KL = (RadioButton) findViewById(R.id.KL);
ZBL = (RadioButton) findViewById(R.id.ZBL);
radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup radioGroup, int wqm) {
if (KB.isChecked()) {
ZMS.setChecked(false);
ZBL.setChecked(false);
KL.setChecked(false);
picture.setImageResource(R.drawable.kb);
}
if (QD.isChecked()) {
ZMS.setChecked(false);
ZBL.setChecked(false);
KL.setChecked(false);
picture.setImageResource(R.drawable.qd);
}
if (MD.isChecked()) {
ZMS.setChecked(false);
ZBL.setChecked(false);
KL.setChecked(false);
picture.setImageResource(R.drawable.md);
}
radio2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup radioGroup, int wqm) {
if (ZMS.isChecked()) {
KB.setChecked(false);
QD.setChecked(false);
MD.setChecked(false);
picture.setImageResource(R.drawable.zms);
}
if (KL.isChecked()) {
KB.setChecked(false);
QD.setChecked(false);
MD.setChecked(false);
picture.setImageResource(R.drawable.kl);
}
if (ZBL.isChecked()) {
KB.setChecked(false);
QD.setChecked(false);
MD.setChecked(false);
picture.setImageResource(R.drawable.zbl);
}
}
});
}
});
}
}
三、效果展示