Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android FrameLayout窃取TextView的触摸事件_Android_Events_Textview_Touch - Fatal编程技术网

Android FrameLayout窃取TextView的触摸事件

Android FrameLayout窃取TextView的触摸事件,android,events,textview,touch,Android,Events,Textview,Touch,我有一个简单的代码: public class MainActivity extends ActionBarActivity { public TextView textView; StringBuilder builder = new StringBuilder(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setCo

我有一个简单的代码:

public class MainActivity extends ActionBarActivity {

public TextView textView;
StringBuilder builder = new StringBuilder();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView = (TextView) findViewById(R.id.touch_textView);
    textView.setText("Hello");


    textView.setOnTouchListener(new View.OnTouchListener() {

        String TAG = "onTouchListener";

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d(TAG,"Triggered");

            // TODO Auto-generated method stub
            switch(event.getAction())
            {
            case MotionEvent.ACTION_DOWN:
                Log.d(TAG,"Down");
                builder.append("down, ");
                break;
            case MotionEvent.ACTION_MOVE:
                builder.append("move, ");
                Log.d(TAG,"Move");
                break;
            case MotionEvent.ACTION_CANCEL:
                builder.append("cancel, ");
                Log.d(TAG,"Cancel");
                break;
            case MotionEvent.ACTION_UP:
                builder.append("up, ");
                Log.d(TAG,"Up");
                break;
            }

            builder.append(event.getX());
            builder.append(", ");
            builder.append(event.getY());
            String text = builder.toString();
            Log.d("TouchTest", text);
            textView.setText(text);         
            return true;
        }
    });
}
}
…这个简单的布局:

<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="com.example.touchhandling.MainActivity">

<TextView
    android:id="@+id/touch_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
</RelativeLayout>
我的问题是,我的TextView的onTouchListener没有接收触摸事件,相反,我在Logcat中看到:

ViewRoot的触控事件:操作启动

似乎我的框架布局正在窃取触摸事件。我怎样才能做到这一点?孩子是否应该首先获得事件

谢谢,任何提示都将不胜感激

尝试textView.setClickabletrue

以及fragment_layout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/touch_parent"
    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="com.example.stacktest.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/touch_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

不,还是一样的行为。如果我执行以下操作:TextView TextView=新建TextView;textView.setOnTouchListener;setContentViewtextView。。。然后听者就可以正常工作了。问题是因为当我使用布局时,这就是为什么我怀疑问题出在textView的根上。我在ActionBarActivity中快速尝试了你的代码,它对我有效。你在活动中还做了什么?发布的xml实际上是R.layout.activity\u main?这就是我所做的。我已经粘贴了整个代码。我尝试了你的代码,它的行为和我的一样。奇怪,我们怎么会有两种不同的行为……你的最小SDK是什么?我的是11蜂巢,但由于您使用的是getSupportFragmentManager,我假设您使用的是较低的minSdk?!这可能是差异的根源吗。。。?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/touch_parent"
    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="com.example.stacktest.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/touch_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>