Detect in android widget a touch event from down to up (or the inverse)

夙愿已清 提交于 2020-01-06 20:25:29

问题


How I can detect a touch event from down to up (or the inverse) in Android widget (sdk 2.3.3)?


回答1:


Try setting an OnTouchListener to the widget and getting the event codes with event.getAction();

Suppose your widget is an ImageView. In xml define:

<ImageView android:id="@+id/imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:src="@drawable/image"/>

and in Java, your code would be:

ImageView mImageView = (ImageView)findViewById(R.id.imageview);

mImageView.setOnTouchListener(new OnTouchListener(){
     @Override
     public boolean onTouch (View v, MotionEvent event){
         if(event.getAction() == MotionEvent.DOWN){
             //do something
         }else if (event.getAction() == MotionEvent.UP){
             // do something
         }else if (event.getAction() == MotionEvent.MOVE){
            //do something
         }
     }
}):



回答2:


You can't. You can only setOnClickListeners

I recommend you to read this great answer. Detect OnTouch on AppWidget. If is it possible?



来源:https://stackoverflow.com/questions/8476362/detect-in-android-widget-a-touch-event-from-down-to-up-or-the-inverse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!