Có bất kỳ loại onPress và onRelease cho các nút android như trong flash không?onPress/onRelease trong Android
Trả lời
Có. Thay vì một onClickListener, bạn phải sử dụng một OnTouchListener. http://developer.android.com/reference/android/view/View.OnTouchListener.html
Ngoài ra, việc này không liên quan gì đến flash và không được gắn thẻ như vậy.
Hãy xem Handling UI Events trên tài liệu android, bạn đặc biệt muốn onTouch nếu bạn muốn nghe xuống và phát hành. Nếu bạn đang làm điều này chỉ để thay đổi giao diện của một nút thì có nhiều cách khác để xử lý nút này bằng cách sử dụng state lists.
thử điều này:
someView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction()==MotionEvent.ACTION_DOWN)
runEnemy();
else
stopEnemy();
return true;
}
});
arg1.getAction() == 0 đó là bình ACTION_DOWN http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_DOWN
Đó là quá muộn, nhưng có lẽ ai đó sẽ tìm thấy nó hữu ích:
mButton.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
Log.d("Pressed", "Button pressed");
else if (event.getAction() == MotionEvent.ACTION_UP)
Log.d("Released", "Button released");
// TODO Auto-generated method stub
return false;
}
});
cảm ơn :) nó đã giúp tôi ~ – Sunny
Bạn có thể thay đổi mButton
, thay vào đó là của bạn là gì. Và bạn có thể thêm ngoặc sau if
và else if
mButton.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
Log.d("Pressed", "Button pressed");
else if (event.getAction() == MotionEvent.ACTION_UP)
Log.d("Released", "Button released");
// TODO Auto-generated method stub
return false;
}
});
và mã này là từ bên cạnh tôi:
// butona basınca bir ses çekince bir ses geliyor.
sescal.setOnTouchListener(new OnTouchListener()
{
@Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
Log.d("Pressed", "Button pressed");
mp.start();
}
else if (event.getAction() == MotionEvent.ACTION_UP)
{
Log.d("Released", "Button released");
mp2.start();
}
// TODO Auto-generated method stub
return false;
}
});
- 1. Enable android: largeHeap trong Android 4, và vô hiệu hóa nó trong Android 2.3
- 2. getApplication trong BroadcastReceiver trong android
- 3. lỗi INSTALL_FAILED_MISSING_SHARED_LIBRARY trong Android
- 4. gọi lại trong android?
- 5. ImageButton trong Android
- 6. Z-index trong android?
- 7. System.exit() trong android
- 8. Custom DatePicker trong Android?
- 9. Ảnh bitmap trong Android
- 10. simple_list_item_2 trong Android
- 11. Android: EditText trong ListView
- 12. Pin Android trong SDK
- 13. iptables trong android
- 14. OnClickListener trong Android Studio
- 15. ServiceConnectionLeaked trong Android
- 16. Android StackOverflowError trong ViewGroup.resetResolvedTextDirection
- 17. tạo json trong android
- 18. OnBackPressed trong Xamarin Android
- 19. MVVMCross Bindings trong Android
- 20. getContentView trong Android
- 21. Android HashMap trong Gói?
- 22. Caching trong webview Android
- 23. static AlarmManager trong Android
- 24. InetAddress trong Android
- 25. Lucene trong Android
- 26. Máy ảnh trong Android
- 27. webview.loadUrl() trong android?
- 28. Webcam USB trong Android
- 29. Android ListView trong Fragment
- 30. PJSip trong android
Nếu bạn thực hiện 'KeyboardView.OnKeyboardActionListener', sau đó bạn sẽ có quyền truy cập cho 'onPress()' và 'onRelease()' –
@IgorGanapolsky là sai và đây là câu hỏi 5 năm cũ ... – nebkat
Tùy thuộc vào cách bạn nhìn vào nó: http://developer.android.com/reference/android/inputmethodservice/KeyboardView.OnKeyboardActionListener .html # onRelease (int). Nhưng không thực sự cho các nút. –