我使用XML文件显示具有2种状态(正常,按下)的按钮。我怎样才能知道何时按下按钮?是否有像onClick --> onPress这样的事件,或者我怎样才能找到它?
这是我使用的 XML 文件:
<?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="false" android:drawable="@drawable/buttonbluepressed" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/buttonbluepressed" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/buttonbluepressed" />
<item android:drawable="@drawable/buttonblue"/>
</selector>
这是我如何使用它:
<Button
android:id="@+id/button_choose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/choose_button"
android:onClick="onButtonClicked"
android:text="@string/button_choose" />
您可以将
setOnTouchListener用作:
button.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//Button Pressed
}
if(event.getAction() == MotionEvent.ACTION_UP){
//finger was lifted
}
return false;
}
});