我有簡單的自定義按鈕:自定義視圖(按鈕)背景選擇不工作
public class myButton extends Button {
public myButton (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
commonConstructorCode();
}
public myButton (Context context, AttributeSet attrs) {
super(context, attrs);
commonConstructorCode();
}
public myButton (Context context) {
super(context);
commonConstructorCode();
}
private void commonConstructorCode() {
this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
if(action == MotionEvent.ACTION_DOWN)
{
//Just to be sure that we removed all callbacks,
// which should have occurred in the ACTION_UP
removeCallbacks(repeatClickWhileButtonHeldRunnable);
//Perform the default click action.
performClick();
//if(v.isEnabled()){
//Schedule the start of repetitions after a one half second delay.
postDelayed(repeatClickWhileButtonHeldRunnable, initialRepeatDelay);
//} else {
// removeCallbacks(repeatClickWhileButtonHeldRunnable);
//}
}
else if(action == MotionEvent.ACTION_UP) {
//Cancel any repetition in progress.
removeCallbacks(repeatClickWhileButtonHeldRunnable);
}
//Returning true here prevents performClick() from getting called
// in the usual manner, which would be redundant, given that we are
// already calling it above.
return true;
}
});
}
}
與選擇
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@drawable/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="@android:color/white" /> <!-- focused -->
<item android:drawable="@drawable/button_standard" /> <!-- default -->
</selector>
通過
<com.blabla.myButton
...
background="@drawable/button_selector" />
背景在 「正常」 設置狀態很好。但其他國家根本不工作。什麼是奇怪的 - 當我改變
<com.blabla.myButton
到
<Button
選擇工作完全正常。
任何想法?
編輯: 我加了commonConstructorCode()。
好吧,這很奇怪。我試着用選擇器XML文件做一個自定義按鈕,一切都很好。 – user2340612 2013-05-07 23:22:02
我已經在Android 4.2和2.3.7的設備上測試過它 - 兩者的結果都是一樣的,錯誤的。 – androfan 2013-05-07 23:30:00
我使用樣式添加選擇器背景。但直接設置不起作用。 – androfan 2013-05-07 23:37:41