xml
<?xml version="1.0" encoding="utf-8"?>
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.myapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please choose a flower Please choose a flower" 让文字滚动 代码是网上找的 第一次发现字数不够就不能滚动于是就输了两遍
android:textSize="24sp"
android:textColor="#0000cd"
android:singleLine="true"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
android:focusable="true"/>
<LinearLayout
android:layout_width="251dp"
android:layout_height="267dp"
android:paddingLeft="20dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/img_hua"
android:layout_width="wrap_content" 增加一行显示图片,给它一个id在后面调用drawable中的图片
android:layout_height="wrap_content"
android:src="@drawable/mudan"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:orientation="horizontal">
<RadioGroup
android:id="@+id/rg_hua1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbtn_mudan"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 使用RadioGroup和RadioButton创建一行三个选项,同上又加了一个变成两行六选项肉点小问题
就是在实现后两行都可以选一个。
android:text="牡丹"
android:textSize="20sp"/>
<RadioButton
android:id="@+id/rbtn_yueji"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="月季"
android:textSize="20sp"/>
<RadioButton
android:id="@+id/rbtn_lanhua"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="兰花"
android:textSize="20sp"/>
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20dp"
android:orientation="horizontal">
<RadioGroup
android:id="@+id/rg_hua2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbtn_qiangwei"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蔷薇"
android:textSize="20sp"/>
<RadioButton
android:id="@+id/rbtn_yinghua"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="樱花"
android:textSize="20sp"/>
<RadioButton
android:id="@+id/rbtn_hehua"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="荷花"
android:textSize="20sp"/>
</RadioGroup>
</LinearLayout>
</RadioGroup>
java
package com.example.administrator.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import static com.example.administrator.myapplication.R.mipmap.mudan;
public class MainActivity extends AppCompatActivity {
private ImageView imgHua;
private RadioGroup rgHua1;
private RadioButton rbtnMudan;
private RadioButton rbtnYueji; 定义组件
private RadioButton rbtnLanhua;
private RadioGroup rgHua2;
private RadioButton rbtnQiangwei;
private RadioButton rbtnYinghuai;
private RadioButton rbtnHehua;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgHua=(ImageView)findViewById(R.id.img_hua);
rgHua1 = (RadioGroup) findViewById(R.id.rg_hua1);
rbtnMudan = (RadioButton) findViewById(R.id.rbtn_mudan);
rbtnYueji = (RadioButton) findViewById(R.id.rbtn_yueji); 获取组件
rbtnLanhua = (RadioButton) findViewById(R.id.rbtn_lanhua);
rgHua2 = (RadioGroup) findViewById(R.id.rg_hua2);
rbtnQiangwei = (RadioButton) findViewById(R.id.rbtn_qiangwei);
rbtnYinghuai = (RadioButton) findViewById(R.id.rbtn_yinghua);
rbtnHehua = (RadioButton) findViewById(R.id.rbtn_hehua);
}
public void onClick(View V){
if(rbtnMudan.isChecked()){
imgHua.setImageResource(R.drawable.mudan);}
else if(rbtnYueji.isChecked()){
imgHua.setImageResource(R.drawable.yueji);}
else if(rbtnLanhua.isChecked()){
imgHua.setImageResource(R.drawable.lanhua);} 监听drawable中的图片 但是这个不会。
else if(rbtnQiangwei.isChecked()){
imgHua.setImageResource(R.drawable.qiangwei);}
else if(rbtnYinghuai.isChecked()){
imgHua.setImageResource(R.drawable.yinghua);}
else if(rbtnHehua.isChecked()){
imgHua.setImageResource(R.drawable.hehua);}
}
}