手指移动到下一个视图时的触摸事件



我拖动手指,它移动到下一个视图。这两个视图都实现了 setOnTouchListener()。

如何发现手指现在在第二视图上。

即使我的手指在第二个视图上,ACTION_MOVE也会返回第一个视图。

我是否需要实现任何其他侦听器?

<LinearLayout 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.textview.MainActivity" >
<TextView
    android:id = "@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
<TextView
    android:id = "@+id/text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello_world1" />
<TextView
    android:id = "@+id/text3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello_world2" />
</LinearLayout>

主活动.java:

package com.example.textview;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView txt1 = (TextView) findViewById(R.id.text1);
    txt1.setOnTouchListener(new OnTouchListener() {         
        @Override
        public boolean onTouch(View v, MotionEvent e) {
            TextView txt1 =  (TextView) findViewById(v.getId());
            Log.d("txt1.getText()",txt1.getText().toString());
            return false;
        }
    });
     txt1 = (TextView) findViewById(R.id.text2);
    txt1.setOnTouchListener(new OnTouchListener() {         
        @Override
        public boolean onTouch(View v, MotionEvent e) {
            TextView txt1 =  (TextView) findViewById(v.getId());
            Log.d("txt1.getText()",txt1.getText().toString());
            return false;
        }
    });

     txt1 = (TextView) findViewById(R.id.text3);
    txt1.setOnTouchListener(new OnTouchListener() {         
        @Override
        public boolean onTouch(View v, MotionEvent e) {
            TextView txt1 =  (TextView) findViewById(v.getId());
            Log.d("txt1.getText()",txt1.getText().toString());
            return false;
        }
    });
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

您可以检查当前的 MotionEvent x 和 y 是否在第二个视图命中矩形中

            Rect viewHitRect = new Rect();
        secondView.getHitRect(viewHitRect);
        if(viewHitRect.contains((int) motionEvent.getX(), (int) motionEvent.getY())) {//You are in 2nd view}

当手指在textview2上时,它会变得ACTION_HOVER_ENTER

textview1得到ACTION_HOVER_EXIT

检查这些参数,ACTION_MOVE将提供给您提到的textview1,但hover参数清楚地决定了此时哪个textfield在手指下

更新:

如果手指被点击views那么你将得到ACTION_DOWN textview2和平行textview1得到ACTION_UP

最新更新