简体   繁体   English

更改按钮状态OnClick

[英]Changing Button State OnClick

In Android, whenever I click a button (namely an ImageButton), I want to give the user feedback by changing the color of the button (maybe inverting colors, or darkening, etc). 在Android中,每当我点击一个按钮(即一个ImageButton)时,我想通过改变按钮的颜色(可能反转颜色或变暗等)来给用户反馈。 At this point, I don't want to change to a new button or anything, just want to change state so as to reflect the click. 此时,我不想更改为新按钮或任何内容,只是想改变状态以反映点击。

I realize I can add a new drawable, so each button would have 2 states (when clicked, I would change to the second state). 我意识到我可以添加一个新的drawable,所以每个按钮将有2个状态(点击时,我会改为第二个状态)。

That is (pseudocode): 那是(伪代码):

onClick { onClick {

  ImageButton.setDrawable(R.drawable.myclickedbutton) 

} }

Is there a better way to do this (I believe iOS will play with ImageColors for you when you click, and that's what I want here in Android), or is what I am thinking the only possibility? 有没有更好的方法来做到这一点(我相信当你点击时iOS会与你一起使用ImageColors,这就是我在Android中想要的),或者我认为唯一的可能性是什么?

I feel I'm missing something major with this concept. 我觉得我错过了这个概念的主要内容。

Thanks in Advance. 提前致谢。

Edit : If it makes a difference, my app runs on minimum target 2.2. 编辑 :如果它有所不同,我的应用程序运行在最低目标2.2。

Edit 2 : For clarification, I'm working with an Image Button which already has a custom drawable resource. 编辑2 :为了澄清,我正在使用一个已经有自定义可绘制资源的图像按钮 That's what I want to manipulate onClick. 这就是我想在onClick上操作的东西。

Edit 3 : I don't think I'm being clear enough here. 编辑3 :我认为我在这里不够清楚。 I know how to change the state using two drawables (whether it be onTouch, XML, onClick, etc.) I was asking if there was a way that Android (or some sort of method) can invert or darken colors for all buttons automatically when clicked (as iOS does, I believe). 我知道如何使用两个drawables(无论是onTouch,XML,onClick等)来改变状态。我问是否有一种方法Android(或某种方法)可以自动反转或变暗所有按钮的颜色点击(就像iOS一样,我相信)。 I am not asking how to do this on a button-to-button basis. 我不是在按钮到按钮的基础上询问如何执行此操作。 This can be accomplished with 2 drawables per button, and I realize that. 这可以通过每个按钮2个抽屉实现,我意识到这一点。

button.xml 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 android:shape="rectangle">
            <corners android:radius="5dp"/>
            <solid android:color="#f00"/>
            <padding android:left="10.0dip" android:top="10.0dip"
                android:right="10.0dip" android:bottom="10.0dip"/>
            <stroke android:width="1.0dip" android:color="#222"/>
        </shape>
    </item>
    <item android:state_pressed="false">
        <shape android:shape="rectangle">
            <corners android:radius="5dp"/>
            <solid android:color="#f1f1f1"/>
            <padding android:left="10.0dip" android:top="10.0dip"
                android:right="10.0dip" android:bottom="10.0dip"/>
            <stroke android:width="1.0dip" android:color="#222"/>
        </shape>
    </item>
</selector>

Apply this button as background 应用此按钮作为背景

<Button
    android:background="@drawable/button"/>

and in your class file do like this 并在您的类文件中这样做

public void onClick(View v) {

  pressedButton.setPressed(true);
}

so that the red color will be stable 这样红色就会稳定

You dont have to use the code you mentioned in order to switch the drawables . 您不必使用您提到的代码来切换drawable。 Rather create a drawable with two different states. 而是创建一个具有两种不同状态的drawable。

As you said you already have custom drawables, you can use them for different states. 正如您所说,您已经拥有自定义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/drawable1" /> <!-- for clicked state-->        
 <item android:drawable="@drawable/drawable2" /> <!-- for normal state--> 
</selector>

place this xml as drawable_1.xml in drawable folder and use this drawable in your code, 将此xml作为drawable_1.xml放在drawable文件夹中,并在代码中使用此drawable,

yourButton.setDrawable(R.drawable.drawable_1)

You can use onTouchListener for button and change the state of button according to button click.. 您可以使用onTouchListener作为按钮,并根据按钮单击更改按钮的状态。

                button.setOnTouchListener(new OnTouchListener() {

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                     if(event.getAction()==MotionEvent.ACTION_DOWN)
                     {
                   // here u can change the button color or image as clickable as 
                   button.setBackgroundImage(R.drawable.image_pressed);

                     }
                     if(event.getAction()==MotionEvent.ACTION_UP)
                     {
                  // here u can change the button color or image as released as 
                   button.setBackgroundImage(R.drawable.image_released); 
                     }
                        return false;
                    }
                });

I think it may helpful to you.. 我认为这对你有帮助..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM