本文介绍了工作不正常onTouch事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图扩展相对布局,并添加一个简单的方法来设置onTouchListener。
I am trying to extend a relative layout, and add a simple method to set an onTouchListener.
问题是,当我设置监听器,也就是被称为唯一的事件是 MotionEvent.ACTION_DOWN
The problem is that after I set the listener, the only event that is being called is MotionEvent.ACTION_DOWN
其他的事件不会被调用。
Other events are not being called.
下面是自定义相对布局的我的code:
Here is my code of the custom relative layout:
public class MyRelativeLayout extends RelativeLayout {
public MyRelativeLayout(Context context) {
super(context);
}
public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public void setOnTouchEvent() {
this.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
Log.d("myTextView", "onTouch event called");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//only this event is being called
return false;
default:
//other events are not being called
return false;
}
}
});
}
}
下面是XML code:
Here is the xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.example.ontouchtest.MyRelativeLayout
android:id="@+id/test"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/hello_world" />
</RelativeLayout>
这是在MainActivity:
And this is the MainActivity:
public class MainActivity extends Activity {
MyRelativeLayout test;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test = (MyRelativeLayout) findViewById(R.id.test);
test.setOnTouchEvent();
}
}
从logcat的打印 MotionEvent
动作时,我得到0:
onTouch事件称为0
推荐答案
的问题是动作下来后返回假
。把它转化为真正
,然后它会通过其他活动了。
The problem is that after action down you return false
. turn it to true
and then it will pass the other events too.
这篇关于工作不正常onTouch事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!