单击时如何在Android中更改按钮的颜色?

And*_*era 75 android

我正在开发Android应用程序.我想让4个按钮水平放置在屏幕的底部.在这4个按钮中,2个按钮上有图像.按钮的边框应为黑色,边框应尽可能薄.当我单击按钮时,我希望按钮的背景应该更改为蓝色而不更改边框的颜色,并且应该保留该颜色一段时间.如何在Android中实现此方案?

met*_*ode 122

一种方法是创建一个这样的XML文件drawable,名为whatever.xml:

<?xml version="1.0" encoding="utf-8"?> 
  <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_focused="true"
        android:state_pressed="true"
        android:drawable="@drawable/bgalt" />

    <item
        android:state_focused="false"
        android:state_pressed="true"
        android:drawable="@drawable/bgalt" />

    <item android:drawable="@drawable/bgnorm" />
</selector>
Run Code Online (Sandbox Code Playgroud)

bgalt并且bgnorm是可绘制的PNG图像.

如果您在活动中以编程方式创建按钮,则可以使用以下方法设置背景:

final Button b = new Button (MyClass.this);
b.setBackgroundDrawable(getResources().getDrawable(R.drawable.whatever));
Run Code Online (Sandbox Code Playgroud)

如果使用XML设置按钮的样式,则可以执行以下操作:

<Button
  android:id="@+id/mybutton"
  android:background="@drawable/watever" />
Run Code Online (Sandbox Code Playgroud)

最后是教程链接.希望这可以帮助.

  • 如果要避免使用图像,还可以使用颜色定义替换XML文件中的那些drawable. (16认同)
  • @ jshin47一次经验性的反复试验.我现在写了两本关于Android开发的书:-) (7认同)

Faa*_*hir 71

使用"bg_button.xml"将此代码保存在drawable文件夹中,并将"@ drawable/bg_button"调用为xml中按钮的背景:

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#004F81" />
            <stroke
                android:width="1dp"
                android:color="#222222" />
            <corners
                android:radius="7dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#89cbee"
                android:endColor="#004F81"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#4aa5d4" />
            <corners
                android:radius="7dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你,使用风格你可以减少应用程序的大小.不使用drawable的好主意 (2认同)

San*_*esh 9

参考这个,

boolean check = false;
Button backward_img;
Button backward_img1;
backward_img = (Button) findViewById(R.id.bars_footer_backward);
backward_img1 = (Button) findViewById(R.id.bars_footer_backward1);
backward_img.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        check = true;
        backward_img.setBackgroundColor(Color.BLUE);
    }
});

if (check == true) {
    backward_img1.setBackgroundColor(Color.RED);
    backward_img.setBackgroundColor(Color.BLUE);
}
Run Code Online (Sandbox Code Playgroud)

  • 这将永久设置BG,也就是说,如果用户单击后退按钮,背景将为红色. (2认同)

小智 8

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- default -->
    <item
        android:state_pressed="false"
        android:state_focused="false">
        <shape
            android:innerRadiusRatio="1"
            android:shape="rectangle" >
            <solid android:color="#00a3e2" />

            <padding
                android:bottom="10dp"
                android:left="10dp"
                android:right="10dp"
                android:top="10dp" />

        </shape>
    </item>

    <!-- button focused -->
    <item
        android:state_pressed="false"
        android:state_focused="true">
        <shape
            android:innerRadiusRatio="1"
            android:shape="rectangle" >
            <solid android:color="#5a97f5" />

            <padding
                android:bottom="5dp"
                android:left="10dp"
                android:right="10dp"
                android:top="5dp" />
        </shape></item>

    <!-- button pressed -->
    <item
        android:state_pressed="true"
        android:state_focused="false">
        <shape
            android:innerRadiusRatio="1"
            android:shape="rectangle" >
            <solid android:color="#478df9"/>
            <padding
                android:bottom="10dp"
                android:left="10dp"
                android:right="10dp"
                android:top="10dp" />
        </shape></item>
</selector>
Run Code Online (Sandbox Code Playgroud)


Wax*_*ren 7

试试这个

    final Button button = (Button) findViewById(R.id.button_id);
    button.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP) {
                button.setBackgroundColor(Color.RED);
            } else if(event.getAction() == MotionEvent.ACTION_DOWN) {
                button.setBackgroundColor(Color.BLUE);
            }
            return false;
        }

    });
Run Code Online (Sandbox Code Playgroud)


Pir*_*hah 6

如果要在按下按钮时更改按钮的背景图像或颜色,则只需复制此代码并粘贴到项目中,如下所示.

      <!-- Create new xml file like mybtn_layout.xml file in drawable -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/pressed" /> <!--pressed --> 
<item android:drawable="@drawable/normal" /> <!-- Normal -->
</selector>
  <!-- Now this file should be in a drawable folder and use this 
  single line code in    button code to get all the properties of this xml file -->

    <Button
      android:id="@+id/street_btn"
      android:layout_width="wrap_content"
      android:background="@drawable/layout_a" > <!-- your required code -->
    </Button>
Run Code Online (Sandbox Code Playgroud)


小智 5

1-为按钮制作1个形状,右键单击可绘制和新的可绘制资源文件。将根元素更改为形状并制作您的形状。

在此处输入图片说明

2-现在从您的形状制作 1 个副本并更改名称并更改纯色。 在此处输入图片说明

3 右键单击​​可绘制和新的可绘制资源文件,只需将根元素设置为选择器。

转到文件并设置“state_pressed”

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"android:drawable="@drawable/YourShape1"/>
<item android:state_pressed="false" android:drawable="@drawable/YourShape2"/>

</selector>
Run Code Online (Sandbox Code Playgroud)

4-最后转到xml布局并设置您的按钮背景“您的选择器”

(抱歉我的英语不好)