更改按钮的android:背景,不同的绘制 [英] Change button android:background to different drawable

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

问题描述

我是新来的Andr​​oid和Java,但已成功地教自己,找到最答案计算器上的问题,而无需提问。到现在为止....

I am new to Android and Java but have managed to teach myself and find most answers to questions on stackoverflow without needing to ask questions. Until now....

下面去,我有很多颜色的按钮,点击后,改变颜色,以各种不同的颜色。

Here goes, I have many colored buttons which, when clicked, change color to a range of different colors.

有例如定义的按钮:

<Button
   android:id="@+id/button17"
   android:layout_width="0dp"
   android:layout_height="fill_parent"
   android:layout_weight="1"
   android:background="@drawable/orange_button"
   android:gravity="center"
   android:onClick="onClick" />

可能有人请告诉我如何改变机器人:使用背景code,以上面的例子变为黄色,例如,单击该按钮时,

Could someone please advise me how to change the android:background using code to change the above example to yellow, for example, when the button is clicked.

在以下clickedButton的code是,我需要改变的背景下按钮的ID。

In the code below clickedButton is the ID of the button for which I need to change the background.

public void onClick(View v) {
    int id=v.getId();
    String clickedButton = getResources().getResourceEntryName(id);

    Change button to Yellow here??

    // Temporary code below to check which button was pressed
    // and convert its number to an integer to eventually access an array

    final TextView tvGameTimer = (TextView) findViewById(R.id.tvGameTimer);
    int buttonNumber = Integer.parseInt(clickedButton.substring(6,8));
    tvGameTimer.setText("" + buttonNumber);

    }

我使用的自定义按钮样式来定义按钮的颜色:

I am using custom button styles to define the button colors:

res/drawable/yellow_button.xml
res/drawable/blue_button.xml
res/drawable/red_button.xml
res/drawable/orange_button.xml
res/drawable/green_button.xml

现在我只需要找出如何从橙色改变按钮黄色。然后,我可以添加逻辑来改变颜色,当应用程序需要。

For now I just need to work out how to change the button from Orange to Yellow. I can then add the logic to change the colors as and when the app requires.

非常感谢您的帮助。

推荐答案

我假设onClick的方法,你发布的是同一个按钮的背景,你正在试图改变。使用此code。

I am supposing that the onClick method you post is of the same Button whose background you are trying to change. Use this code.

  v.setBackgroundResource(R.drawable.yellow_button);

如果的onClick是不一样的按钮的方法然后,用

If onClick is not the method of the same button then, use

  findViewById(R.id.button17).setBackgroundResource(R.drawable.yellow_button);

这篇关于更改按钮的android:背景,不同的绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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