安卓作业-34-贾开芹
安卓作业之跑马灯
一、上网查资料有关此问题的解决方案
二、着手开始写代码:
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
http://schemas.android.com/apk/res/android"
xmlns:tools="
http://schemas.android.com/tools"
android:id="@+id/activity_flower"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.jkq.myapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/holo_green_dark"
android:layout_gravity="center"
android:scrollHorizontally="true"
android:scrollbars="horizontal"
android:text="喜欢哪朵花就点吧"
android:singleLine="true"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"
android:textSize="60sp"
android:id="@+id/timu" />
<ImageView
android:layout_marginTop="40dp"
android:layout_width="230dp"
android:layout_height="160dp"
android:layout_gravity="center"
android:id="@+id/picture"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:id="@+id/zhonglei">
<RadioGroup
android:layout_marginTop="60dp"
android:id="@+id/rg_rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/laba"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:layout_marginRight="@dimen/activity_vertical_margin"
android:checked="true"
android:text="喇叭花"/>
<RadioButton
android:id="@+id/guang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="广玉兰"/>
<RadioButton
android:id="@+id/ying"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="樱花"/>
</RadioGroup>
<RadioGroup
android:layout_marginTop="60dp"
android:id="@+id/rg_rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/mudan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="牡丹花"/>
<RadioButton
android:id="@+id/meigui"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="玫瑰花"/>
<RadioButton
android:id="@+id/baihe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="百合花"/>
</RadioGroup>
</RadioGroup>
</LinearLayout>
<LinearLayout xmlns:android="
xmlns:tools="
android:id="@+id/activity_flower"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.jkq.myapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/holo_green_dark"
android:layout_gravity="center"
android:scrollHorizontally="true"
android:scrollbars="horizontal"
android:text="喜欢哪朵花就点吧"
android:singleLine="true"
android:ellipsize="marquee"
android:focusableInTouchMode="true"
android:focusable="true"
android:marqueeRepeatLimit="marquee_forever"
android:textSize="60sp"
android:id="@+id/timu" />
<ImageView
android:layout_marginTop="40dp"
android:layout_width="230dp"
android:layout_height="160dp"
android:layout_gravity="center"
android:id="@+id/picture"/>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:id="@+id/zhonglei">
<RadioGroup
android:layout_marginTop="60dp"
android:id="@+id/rg_rg1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/laba"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:layout_marginRight="@dimen/activity_vertical_margin"
android:checked="true"
android:text="喇叭花"/>
<RadioButton
android:id="@+id/guang"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="广玉兰"/>
<RadioButton
android:id="@+id/ying"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="樱花"/>
</RadioGroup>
<RadioGroup
android:layout_marginTop="60dp"
android:id="@+id/rg_rg2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/mudan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="牡丹花"/>
<RadioButton
android:id="@+id/meigui"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="玫瑰花"/>
<RadioButton
android:id="@+id/baihe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:text="百合花"/>
</RadioGroup>
</RadioGroup>
</LinearLayout>
JAVA:
package com.example.jkq.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.TextView;
public class MainActivity extends AppCompatActivity {
private TextView timu;
private ImageView picture;
private RadioGroup rg_rg1;
private RadioGroup zhonglei;
private RadioButton laba;
private RadioButton guang;
private RadioButton ying;
private RadioGroup rg_rg2;
private RadioButton mudan;
private RadioButton meigui;
private RadioButton baihe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timu= (TextView) findViewById(
R.id.timu);
laba = (RadioButton) findViewById(
R.id.laba);
guang = (RadioButton) findViewById(
R.id.guang);
ying = (RadioButton) findViewById(
R.id.ying);
mudan = (RadioButton) findViewById(
R.id.mudan);
meigui = (RadioButton) findViewById(
R.id.meigui);
baihe= (RadioButton) findViewById(
R.id.baihe);
rg_rg1 = (RadioGroup) findViewById(
R.id.rg_rg1);
rg_rg2 = (RadioGroup) findViewById(
R.id.rg_rg2);
zhonglei = (RadioGroup) findViewById(
R.id.zhonglei);
picture=(ImageView)findViewById(
R.id.picture);
rg_rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(laba.isChecked()){
picture.setImageResource(R.drawable.喇叭花);
rg_rg2.clearCheck();
}
if(guang.isChecked()){
picture.setImageResource(R.drawable.广玉兰);
rg_rg2.clearCheck();
}
if(ying.isChecked()){
picture.setImageResource(R.drawable.樱花);
rg_rg2.clearCheck();
}
}
});
rg_rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(mudan.isChecked()){
picture.setImageResource(R.drawable.牡丹花);
rg_rg1.clearCheck();
}
if(meigui.isChecked()){
picture.setImageResource(R.drawable.玫瑰花);
rg_rg1.clearCheck();
}
if(baihe.isChecked()){
picture.setImageResource(R.drawable.百合花);
rg_rg1.clearCheck();
}
}
});
}
}
期间遇到一个很大的问题,就是运行的时候会只跳出来图片
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.TextView;
public class MainActivity extends AppCompatActivity {
private TextView timu;
private ImageView picture;
private RadioGroup rg_rg1;
private RadioGroup zhonglei;
private RadioButton laba;
private RadioButton guang;
private RadioButton ying;
private RadioGroup rg_rg2;
private RadioButton mudan;
private RadioButton meigui;
private RadioButton baihe;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
timu= (TextView) findViewById(
laba = (RadioButton) findViewById(
guang = (RadioButton) findViewById(
ying = (RadioButton) findViewById(
mudan = (RadioButton) findViewById(
meigui = (RadioButton) findViewById(
baihe= (RadioButton) findViewById(
rg_rg1 = (RadioGroup) findViewById(
rg_rg2 = (RadioGroup) findViewById(
zhonglei = (RadioGroup) findViewById(
picture=(ImageView)findViewById(
rg_rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(laba.isChecked()){
picture.setImageResource(R.drawable.喇叭花);
rg_rg2.clearCheck();
}
if(guang.isChecked()){
picture.setImageResource(R.drawable.广玉兰);
rg_rg2.clearCheck();
}
if(ying.isChecked()){
picture.setImageResource(R.drawable.樱花);
rg_rg2.clearCheck();
}
}
});
rg_rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if(mudan.isChecked()){
picture.setImageResource(R.drawable.牡丹花);
rg_rg1.clearCheck();
}
if(meigui.isChecked()){
picture.setImageResource(R.drawable.玫瑰花);
rg_rg1.clearCheck();
}
if(baihe.isChecked()){
picture.setImageResource(R.drawable.百合花);
rg_rg1.clearCheck();
}
}
});
}
}
期间遇到一个很大的问题,就是运行的时候会只跳出来图片
三、运行的结果如下图所示:
![](https://images2015.cnblogs.com/blog/1130396/201703/1130396-20170321155736690-286005700.png)
然后第二个作业实在在我的能力范围之外