在单个onclick

时间:2017-10-10 13:39:16

标签: android

令人惊讶的事实的主要活动

package com.example.user.amazingfacts;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private TextView showtext;
private Button showbutton;
private RelativeLayout relativeLayout;
private Amazingnote Amazingnote=new Amazingnote();
private Colorwheel colorwheel=new Colorwheel();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showtext=(TextView)findViewById(R.id.showtext);
        showbutton=(Button)findViewById(R.id.showbutton);
        showbutton.setOnClickListener(new  View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
String fact= Amazingnote.getFact();
showtext.setText(fact);

int color=colorwheel.getColor();

  relativeLayout.setBackgroundColor(color);
showbutton.setTextColor(color);


            }
        });
    }
}

惊人事实的第二项活动 - 令人惊叹的注意事项

package com.example.user.amazingfacts;

import java.util.Random;

/**
 * Created by User on 09-10-2017.
 */

public class Amazingnote {
    public String getFact() {
        String[] facts = {"In 1889, the Queen of Italy, Margherita Savoy, ordered the first pizza delivery",
                " You can buy eel flavored ice cream in Japan",
                "Although the bobcat is rarely seen, it is the most common wildcat in North America",
                " A cat's tail contains nearly 10 percent of all the bones in its body",
                "The term astronaut comes from Greek words that mean star and sailor",
                "The calcium in our bones and the iron in our blood come from ancient explosions of giant stars",
                "The Nile crocodile can hold its breath underwater for up to 2 hours while waiting for prey",
                "Jellyfish, or jellies as scientists call them, are not fish. They have no brain, no heart, and no bones",
                " Some people used to believe that kissing a donkey could relieve a toothache.",
                " Because the speed of Earth's rotation changes over time, a day in the age of dinosaurs was just 23 hours long",
                "Hummingbirds' wings can beat 200 times a second.",
                "There are more than 1,200 water parks in North America.",
                "A seahorse can move its eyes in opposite directions—all the better to scan the water for food and predators.",
                " To cook an egg, a sidewalk needs to be 158°F."

        };

        Random randomGenerator=new Random();
        int randomnumber=randomGenerator.nextInt(facts.length);
        return facts[randomnumber];

    }
}

令人惊叹的事实 - 色彩轮的第三次活动

package com.example.user.amazingfacts;

import android.graphics.Color;

import java.util.Random;

/**
 * Created by User on 10-10-2017.
 */

public class Colorwheel
{
  //Fields or Member Variables -Properties abou the object
    private String[] colors = {
          "#39add1",//light blue
          "#3079ab",//dark blue
          "#c25975",//mauve
          "#e15258",//red


    };
    //Methods-Actions the object can take
    int getColor()
    {

    Random randomGenerator=new Random();
    int randomnumber=randomGenerator.nextInt(colors.length);
     int color= Color.parseColor(colors[randomnumber]) ;
    return color ;

}
}

这是一个名为Amazing facts.的小应用程序。它使用两个Text views并且只有一个按钮。     第一个Text view不是那么重要的一个"你知道吗?"。这是第一个Text view。     当用户单击按钮时,第二个Text View的内容和应用程序的背景颜色会发生变化。这是我的想法。 我使用构建功能成功构建应用程序,但我尝试在我的Android手机上安装应用程序。当我点击按钮时,应用程序停止。我知道on click属性中存在一些问题。我完全删除了二等"色轮"点击*上的*,然后应用正在运行。为什么应用没有在一个On click中加载两个对象的方法?为什么我无法同时更改文本视图和背景颜色?

1 个答案:

答案 0 :(得分:0)

将主要活动的第一部分更改为

public class MainActivity extends AppCompatActivity {
private TextView showtext;
private Button showbutton;
private RelativeLayout relativeLayout;
private Amazingnote Amazingnote=new Amazingnote();
private Colorwheel colorwheel=new Colorwheel();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    relativeLayout = (RelativeLayout) findViewById(R.layout.main_activity); 
        .
        .
        .

您的布局未初始化,因此您尝试更改尚不存在的对象的颜色。