在Android 应用程序的图形界面应用程序,都是通过事件来实现人机交互的。事件就是用户对图形界面的操作。在Android手机和平板电脑上,主要包括键盘事件和触摸事件两大类。键盘事件包括按下、弹起等。触摸事件包括按下,弹起,滑动,双击等。下面就来介绍android中是怎样对这些事件的处理的:
处理键盘事件:
物理按键的简介:
各个物理按键能够触发的事件及其说明如下所示:
Android中控件在处理物理按键事件是,提供的回调方法有onKeyUP(), onKeyDown()和onKeyLongPress()。
实例代码:
屏蔽后退键:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_MENU){
Toast.makeText(this, "KEYCODE_HOME", Toast.LENGTH_SHORT).show();
return true;
}
return super.onKeyDown(keyCode, event);
}
处理触摸事件:
现在大部分手机都是触摸式手机,那么Android中是如何来处理这些触摸事件的呢?
触摸事件一般有OnClickListener和OnLongClickListener,OnTouchListener 监听器接口. 在控件中的添加是通过setOnClickListener , setOnLongClickListener , setOnTouchListener等等.
手势的创建于识别:
布局文件代码了:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<android.gesture.GestureOverlayView
android:id="@+id/gov"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:gestureColor="#ff0000" >
</android.gesture.GestureOverlayView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="sure"
android:text="确定" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="cancel"
android:text="取消" />
</LinearLayout>
</LinearLayout>
Activity中的代码:
package com.jky.gesture.main;
import java.util.ArrayList;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnGestureListener
{
GestureOverlayView gov;
GestureLibrary library;
Gesture gesture;
EditText tv;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (EditText) findViewById(R.id.tv);
gov = (GestureOverlayView) findViewById(R.id.gov);
//从raw里获取gesturelibrary
library = GestureLibraries.fromRawResource(this, R.raw.gestures);
//加载library
library.load();
gov.addOnGestureListener(this);
}
public void sure(View v){
//清除手势
gov.clear(true);
recognize();
}
public void cancel(View v){
gov.clear(true);
}
@Override
public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
// TODO Auto-generated method stub
Log.i("INFO", "started");
}
@Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
// TODO Auto-generated method stub
Log.i("INFO", "gesturing");
//获取手势
gesture = overlay.getGesture();
}
@Override
public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
// TODO Auto-generated method stub
Log.i("INFO", "canceled");
}
public void recognize(){
//识别手势,从library中,library可以在模拟器的GetsuresBuilder应用生成
ArrayList<Prediction> predictions = library.recognize(gesture);
for(Prediction prediction:predictions){
Log.i("INFO", "识别度:"+prediction.score+"--name:"+prediction.name);
}
}
}
以上只是对Android中的事件做一个基本的描述,其实android事件的处理远远不止这么简单.哈哈....