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>
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>
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);
}
}
备注:
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>
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>
/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>
ViewControllerOnTouchEventActivity.java
package com.example.helloword; import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView; public class ViewControllerOnTouchEventActivity extends Activity {
private TextView textViewPosition;
private View vTouchPanel; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewcontrollerontouchevent);
this.textViewPosition = (TextView) this
.findViewById(R.id.textViewPosition);
this.vTouchPanel = this.findViewById(R.id.vTouchPanel); this.vTouchPanel.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
textViewPosition.setText("x=" + motionEvent.getX() + ",y="
+ motionEvent.getY());
} // 离开该空间范围就完成事件,不再向上触发。
return true;
}
});
} }