当点击或触摸文本视图时,我希望更改其背景颜色。 这是一个文本视图。
<com.example.shuvo.KeyboardButton
android:id="@+id/showMenuButton"
style="@drawable/color"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2"
android:alpha="0.8"
android:text="Menu"
android:textColor="@drawable/text_link_selector"
android:background="@drawable/keyboard"
/>
在keyboard.xml文件中。
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"> <color android:color="#00ff00" /> </item>
And in keyboardButton.java
public class KeyboardButton extends TextView {
public KeyboardButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public KeyboardButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public KeyboardButton(Context context) {
super(context);
}
public void setLayoutParams(int width, int height) {
LayoutParams params = new LayoutParams(width, 60);
this.setLayoutParams(params);
setBackgroundResource(R.drawable.keyboard);
setPadding(10, 10, 10, 10);
}
这个不起作用。问题出在哪里?
onTouch()
的代码在哪里? - Murtaza Khursheed Hussainandroid:clickable="true"
即可。 - Niloy Shuvo