查看在preSS preSS:在preSS更改背景颜色?如何显示该视图是被pressed?视图、看在、颜色、背景

2023-09-06 00:35:19 作者:努力是为自已将来铺路

嘿, 我有,暂时,与9补丁图像边框的自定义视图。

Hey, I have, for the time being, a custom view with a 9-patch image as a border.

这是自定义视图放置三次在LinearLayout中,所以它看起来是这样的:

That custom view is placed three times in a LinearLayout, so it looks like this:

+------------------------+
|  CustomView            |
+------------------------+
|  CustomView            |
+------------------------+
|  CustomView            |
+------------------------+

我附上一个点击事件监听器查看,所以它是可以点击的。但后来我点击它,我不能看到,我点击了它 - 没有颜色没有变化。

I have attached a click event listener to the View, so it is clickable. But then I click it, I cant see that I am clicking it - there is no change in color.

所以,林认为我会附上一份关于preSS监听器,然后更改视图的背景,但我无法找到这样的监听器。

So, Im thought that I'd attach a "onPress" listener, and then change the background of the view but I couldnt find such a listener.

所以,问题是 - 如何创建视图上的行为,这样我就可以看到它正在pressed?这是在Android的正常完成一个绿色的背景,表示它现在是pressed。

So, the question is - how do I create the behaviour on the View so I can see that it is being pressed? this is normally done in Android with a green background to indicate that it is now being pressed.

问候

推荐答案

您可以设置OnClickListener的观点。该视图被点击时会被调用。但是,对于一些简单的改变背景当一个视图被点击,你应该使用一个有状态的绘制。他们的工作就是这样,你做3 9片图像。

You could set the OnClickListener for the view. That will be called when the view is clicked. But for something as simple as changing the background when a view is clicked you should use a stateful drawable. They work like this, you make 3 9-patch images.

就像你现在有什么正常的背景。 的背景应该是什么,当用户选择与跟踪球/ D-垫的观点类似 认为应该看什么,当用户点击它像

然后,创建你的文件夹绘制一个新的XML文件。它应该是这样的:

Then you create an new xml file in your drawable folder. It should look like this:

<?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/background_pressed" />
<item
    android:state_focused="true"
    android:state_pressed="false"
    android:drawable="@drawable/background_focused" />
<item
    android:state_focused="false"
    android:state_pressed="true"
    android:drawable="@drawable/background_pressed" />
<item
    android:drawable="@drawable/background_normal" />

然后,当你设置视图的背景设置到XML文件。

Then, when you set the background of your view set it to the xml file.