按钮,点击更改背景颜色? [英] Change background color on button click?

查看:216
本文介绍了按钮,点击更改背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的第二个活动的背景颜色改变一次按钮pssed $ P $。这是my_activity2.xml:

I want the background color of my second activity to change once a button is pressed. This is my_activity2.xml:

<RelativeLayout 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"
tools:context=".MyActivity2"
android:background="#ffdb4b5e">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Next"
    android:id="@+id/button"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:textColor="#ffffffff"
    android:textSize="72sp"
    android:background="#00000000"
    />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="@string/thing1"
    android:id="@+id/textView"
    android:textColor="#ffffffff"
    android:textSize="36sp"
    android:gravity="center"
    android:layout_above="@+id/button"
    />

这是我的MyActivity2.java:

This is my MyActivity2.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_activity2);
    Button n = (Button) findViewById(R.id.button);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
    n.setTypeface(typeface);
    final TextView tv = (TextView) findViewById(R.id.textView);
    Typeface face = Typeface.createFromAsset(getAssets(),
            "OSP-DIN.ttf");
    tv.setTypeface(face);

 final String[] values = getResources().getStringArray(R.array.things_array);
n.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Random RAND=new Random();
        String nextValue = values[RAND.nextInt(values.length)];
        tv.setText(nextValue);

我存储在strings.xml中像这样的背景颜色:

I have the background colors stored in the strings.xml like so:

<?xml version="1.0" encoding="utf-8"?>

<string-array name="colorcode_array">
    <item>3498db</item>
    <item>2ecc71</item>
    <item>9b59b6</item>
    <item>f1c40f</item>
    <item>1abc9c</item>
    <item>2980b9</item>
    <item>8e44ad</item>
    <item>e41c1c</item>
    <item>2ecca9</item>
    <item>752ecc</item>
    <item>4f2ecc</item>
    <item>2eccc3</item>
    <item>2ecc53</item>
    <item>2ecc2e</item>
    <item>5bcc2e</item>
    <item>9ecc2e</item>
    <item>cca12e</item>
    <item>cc712e</item>
    <item>f1c209</item>
    <item>86f109</item>
    <item>f11616</item>
    <item>9c1818</item>
</string-array>

现在我怎样才能使随机的背景颜色变化单击下一步按钮的时候?马修

Now how can I make the background color change on random when the button "Next" is clicked? Matthew

编辑:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my_activity2);
    final RelativeLayout layout = (RelativeLayout) findViewById(R.id.my_relative_layout);
    Button n = (Button) findViewById(R.id.button);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
    n.setTypeface(typeface);
    final TextView tv = (TextView) findViewById(R.id.textView);
    Typeface face = Typeface.createFromAsset(getAssets(),
            "OSP-DIN.ttf");
    tv.setTypeface(face);

    final String[] values = getResources().getStringArray(R.array.things_array);
    final String[] value = getResources().getStringArray(R.array.colorcode_array);
n.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Random RAND=new Random();
        String nextValue = values[RAND.nextInt(values.length)];
        String newValue = value[index++];
        tv.setText(nextValue);
        layout.setBackgroundColor(Color.parseColor(newValue));
    }
});

}

推荐答案

所有的数组首先是格式错误,因为你正在使用十六进制应前有井号标签十六进制颜色,所以你可以在code后解析它。

First of all your array is in wrong format since you are using hex it should have hashtag before the hex color so you can parse it later in the code.

示例:

所有的颜色改变这种格式

change all your colors to this format

<string-array name="colorcode_array">
<item>#3498db</item>
<item>#2ecc71</item>
<item>#9b59b6</item>
<item>#f1c40f</item>
<item>#1abc9c</item>
   .
   .

在你的布局,你需要有一个参考的ID在你的 RelativeLayout的,所以你需要在其上添加一个 ID

In your layout you need to have a reference id in your RelativeLayout so you need to add an id on it

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/my_relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity2"
android:background="#ffdb4b5e">

然后引用它在你的的onCreate 一样的,你在你的按钮做了什么

RelativeLayout layout = (RelativeLayout) findViewById(R.id.my_relative_layout);

现在改变背景颜色你的 RelativeLayout的您现在需要使用解析它的 Col​​or.parseColor 并设置参照背景 RelativeLayout的您创建如上所述。

Now to change the color of the background of your RelativeLayout you now need to parse it using the Color.parseColor and set the Background of the reference to the RelativeLayout that you create as stated above.

示例:

@Override
public void onClick(View v) {
    Random RAND=new Random();
    String nextValue = values[RAND.nextInt(values.length)];
    layout.setBackgroundColor(Color.parseColor(nextValue));
}

编辑:

创建整型全局实例

int index = 0;

在您的onClick增加它

in your onClick increment it

@Override
public void onClick(View v) {
    String nextValue = values[index++];
    layout.setBackgroundColor(Color.parseColor(nextValue));
}

编辑#2:

int index = 0;
    @Override
protected void onCreate(Bundle savedInstanceState) {

这篇关于按钮,点击更改背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆