[英]how can i detect when a button is clicked but not released in android
我正在尋找一種方法來找出如何在單擊但未松開按鈕時通過使用Java代碼來更改android中按鈕的顏色,很高興能得到幫助。
button.setOnTouchListener(new View.OnTouchListener() {
public void onTouch(View view, MotionEvent e) {
switch(e.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println(" mouse pressed ");
break;
case MotionEvent.ACTION_UP:
System.out.println(" mouse released");
break;
}
}
});
您可以使用MotionEvent.ACTION_DOWN情況來處理按鈕顏色更改邏輯。
希望對您有幫助
如果你知道了,通知我:)
在Android文檔中查看文檔 。 它顯示按鈕具有不同的狀態,您可以在每個狀態下對齊顏色。 還要檢查此鏈接
您可以像下面的代碼那樣使用選擇器
在drawable中創建selet.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selector style for listrow -->
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/gradient_bg" />
<item android:state_pressed="true"
android:drawable="@drawable/gradient_bg_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/gradient_bg_hover" />
</selector>
現在如下設置按鈕的背景
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/selector" />
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.