更改按钮的背景触摸 [英] Change button background on touch
问题描述
我要改变绿色背景上的按钮时,这是感动,而无需使用OnTouchListener。我有这样的情况:我把一些butons中的层,然后该层在另一个层,最后一层在onother层。当我触摸按钮的背景改变(使用IM OnTouchListener现在),但如果我拖动手指按键之外,然后把它弄出来的画面按钮的背景仍然是从国家的形象时,它的触摸(否则,如果我点击该按钮,在finnger关闭按钮,它是k为背景正在发生变化)
i want to change the green background of a button when this is touched without using OnTouchListener. i have this situation: i put some butons in a layer, then the layer in another layer, and the last layer in onother layer. when i touch the button the background is changing(i m using OnTouchListener right now) but if i drag the finger outside of the button and then get it out of the screen the background of the button remains the image from the state when it s touch(otherwise if i click the button and the the finnger off the button it is k the background is changing)
推荐答案
1 prepare 3图像按钮状态,并把它变成资源/绘制
文件夹。
1. Prepare 3 images for button states, and put it into resource/drawable
folder.
2 创建 RES /绘制/
文件夹,一个新的XML文件,以任何你想要的名称,在这种情况下,我们只给一个名称为 my_button.xml
。确定该文件的按钮状态是属于哪个形象。
2. create a new XML file in res/drawable/
folder, in whatever name you want, in this case, we just give a name as my_button.xml
. This file defined which button state is belong to which image.
现在,你可以通过这个ID是指这个按钮:@绘制/ my_button
Now, you can refer to this button via this Id : @drawable/my_button.
文件:RES /绘制/ my_button.xml
File : res/drawable/my_button.xml
使用这样的按钮图像 my_button.xml
中创建XML文件绘制
文件夹
create xml file using the button image like this with my_button.xml
in drawable
folder
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_pressed_yellow"
android:state_pressed="true" />
<item android:drawable="@drawable/button_focused_orange"
android:state_focused="true" />
<item android:drawable="@drawable/button_normal_green" />
</selector>
添加一个普通按钮,经由
add a normal button, and attach the background image to above "my_button" via
android:background:@drawable/my_button
这篇关于更改按钮的背景触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!