当用户触摸此按钮时,如何更改按钮中的彩色文字?
这是我的shape.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">
<gradient android:startColor="#ffcc33"
android:endColor="#ffcc33"
android:angle="270" />
<corners android:radius="4dp" />
<stroke android:width="2px" android:color="#FFFFFF" />
</shape>
</item>
<item android:state_focused="true" >
<shape android:shape="rectangle">
<gradient android:startColor="#ffcc33"
android:endColor="#ffcc33"
android:angle="270" />
<corners android:radius="4dp" />
<stroke android:width="2px" android:color="#FFFFFF" />
</shape>
</item>
<item >
<shape android:shape="rectangle">
<gradient android:startColor="#333333"
android:endColor="#333333"
android:angle="270" />
<corners android:radius="4dp" />
<stroke android:width="2px" android:color="#FFFFFF" />
</shape>
</item>
</selector>
答案 0 :(得分:8)
使用颜色选择器,类似这样
<强> 的src /颜色/ button_text.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#000000" />
<item android:state_focused="true" android:color="#000000" />
<item android:color="#FFFFFF" />
</selector>
然后在按钮中执行此操作
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/text"
android:textColor="@color/button_text" />
答案 1 :(得分:0)
按下按钮后你可能会想要做其他事情,所以为什么不创建一个普通按钮并将其背景设置为你的形状然后在java中为按钮创建一个onTouch事件
Button.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
// do other stuff
Button.setTextColor(Color.parseColor("#000000"));
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
Button.setTextColor(Color.parseColor("#FFFFFF"));
}
return false;
}
});