Android:触屏事件

Android触屏事件包含两种:

1)屏幕触屏事件:重写onTouchEvent(MotionEvent event);

2)控件触屏事件:给控件注册触屏事件,setOnTouchEventListener(...)。

屏幕触屏事件

效果:

代码:

res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#FF0000</color>
    
</resources>
View Code

res/layout/activity_main.xml

<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" >

    <TextView
        android:id="@+id/tvPosition"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="30dp"
        android:textColor="@color/red" />
    
</RelativeLayout>
View Code

MainActivity.java

package com.example.helloword;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.widget.TextView;

public class MainActivity extends Activity {
    private TextView tvPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.tvPosition = (TextView) this.findViewById(R.id.tvPosition);
    }

    @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 onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();

        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            Log.i("Test", "fired onTouchEvent: DOWN. x=" + x + ",y=" + y);
            break;
        case MotionEvent.ACTION_MOVE:
            Log.i("Test", "fired onTouchEvent: MOVE. x=" + x + ",y=" + y);
            this.tvPosition.setText("x=" + x + ",y=" + y);
            break;
        case MotionEvent.ACTION_UP:
            Log.i("Test", "fired onTouchEvent: UP. x=" + x + ",y=" + y);
            break;
        }

        // 默认:返回值为false,表示该事件还未触发完成,将继续向上执行。
        return super.onTouchEvent(event);
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        // 当点击回退时,弹出该窗口(也就相当于关闭操作)
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            new AlertDialog.Builder(this).setTitle("是否退出?")
                    .setPositiveButton("确定", new OnClickListener() {
                        @Override
                        public void onClick(DialogInterface arg0, int arg1) {
                            finish();
                        }
                    }).setNegativeButton("取消", null).show();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }
}
View Code

备注:

1)监听触屏事件包含三个Action:Up/Down/Move;

2)默认覆盖屏幕事件返回值为false,表示该事件还未结束,将会继续向上触发。

控件触屏事件

1)只有在注册了setOnTouchEventListener事件的空间上触发事件时,才能响应触屏事件;

2)效果:

3)代码:

res/values/colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="red">#FF0000</color>    
    <color name="yellow">#FFFF00</color>
</resources>
View Code

res/layout/activity_viewcontrollerontouchevent.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <View
        android:id="@+id/vTouchPanel"
        android:layout_width="wrap_content"
        android:layout_height="250dp"
        android:background="@color/yellow" />

    <TextView
        android:id="@+id/textViewPosition"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/red" />

</LinearLayout>
View Code

/AndroidManifest.xml,将首页activity_main.xml修改为activity_viewcontrollerontouchevent.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.helloword"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.helloword.ViewControllerOnTouchEventActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
View Code

ViewControllerOnTouchEventActivity.java

 1 package com.example.helloword;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.MotionEvent;
 6 import android.view.View;
 7 import android.widget.TextView;
 8 
 9 public class ViewControllerOnTouchEventActivity extends Activity {
10     private TextView textViewPosition;
11     private View vTouchPanel;
12 
13     @Override
14     protected void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.activity_viewcontrollerontouchevent);
17         this.textViewPosition = (TextView) this
18                 .findViewById(R.id.textViewPosition);
19         this.vTouchPanel = this.findViewById(R.id.vTouchPanel);
20 
21         this.vTouchPanel.setOnTouchListener(new View.OnTouchListener() {
22             @Override
23             public boolean onTouch(View view, MotionEvent motionEvent) {
24                 if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
25                     textViewPosition.setText("x=" + motionEvent.getX() + ",y="
26                             + motionEvent.getY());
27                 }
28                 
29                 // 离开该空间范围就完成事件,不再向上触发。
30                 return true;
31             }
32         });
33     }
34 
35 }
原文地址:https://www.cnblogs.com/yy3b2007com/p/7770456.html