在我的Android应用程序有绿色的彩色背景一个圆角矩形按钮。我这样做使用.xml文件
In my android app there is a rounded rectangle button with green colored background. i did this using .xml file
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="10dp"
android:shape="rectangle" >
<solid android:color="#B5D397" />
<corners
android:bottomLeftRadius="10dp"
android:bottomRightRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp" />
</shape>
和
android:background="@drawable/rounded_btn"
在布局文件
但是当我preSS按钮并没有显示出任何影响(不变化的颜色),所以我用
but when i press button is wasn't showing any effect(no change is color) so i used
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Button view = (Button) v;
view.getBackground().setColorFilter(0x77000000, PorterDuff.Mode.SRC_ATOP);
和pssing它$ P $后按钮变为暗绿色。到这里一切正常,但问题是释放按钮颜色保持墨绿色之后。我希望它像,因为它是$ P $之前pssing.I提到几个例子说要使用选择在.xml文件即
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:color="#c0c0c0"
android:state_selected="true"/>
<item
android:color="#ffffff"
android:state_pressed="true"/>
<item
android:color="#9A9A9A"
android:state_focused="false"
android:state_pressed="false"
android:state_selected="false"/>
</selector>
这也需要 的android:背景=@绘制/ btn_state
但我已经使用的 的android:背景= @绘制/ rounded_btn
Which also needs android:background="@drawable/btn_state"
but i have already used android:background=@drawable/rounded_btn
因此,如何给这两个效应在一起
So how to give both effect together
我也尝试过使用的 OnTouchListener
i also tried using OnTouchListener
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// show interest in events resulting from ACTION_DOWN
if(event.getAction()==MotionEvent.ACTION_DOWN) return true;
// don't handle event unless its ACTION_UP so "doSomething()" only runs once.
if(event.getAction()!=MotionEvent.ACTION_UP) return false;
doSomething();
button.setPressed(true);
return true;
}
});
但禁用我的 OnclickListener()
方式,我不希望使用 OnTouchListener()
but this disables my OnclickListener()
method and i dont want to use OnTouchListener()
我知道这是愚蠢的,但我是新来的Android非常感谢
i know this is silly but i am new to android thanks a lot
请两个不同形状的XML。一类带绿的颜色,用另一种颜色等..
Make two different shape xml. one with green color and other using another color..
而在selector.xml使用它们
And use them in selector.xml
<item android:drawable="@drawable/rounded_btn_green" android:state_selected="true"/>
<item android:drawable="@drawable/rounded_btn_green" android:state_pressed="true"/>
<item android:drawable="@drawable/rounded_btn_green" android:state_focused="true"/>
<item android:drawable="@drawable/rounded_btn"/>
上一篇:奇怪FileLoadException当加载使用Assembly.ReflectionOnlyLoadFrom WPF项目装配引用加载、奇怪、项目、FileLoadException
下一篇:Android的凌空POST请求 - 解决方法JsonArrayRequest解决方法、Android、POST、JsonArrayRequest