Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android觸摸屏單擊測試

Android觸摸屏單擊測試

編輯:Android開發實例


       我們今天要學習的就是android游戲開發必須對手機的各個設備非常了解。其中最重要的設備之一就是觸摸屏。所以我們這裡重點研究觸摸屏的單擊。

       我們直接看代碼吧。這裡是觸摸屏單擊的測試代碼:

java代碼:
package eoe.gamedev;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.TextView;

public class SingleTouchTest extends Activity implements OnTouchListener{
StringBuilder builder = new StringBuilder();
TextView textView;
public void onCreate(Bundle state){
super.onCreate(state);
textView = new TextView(this);
textView.setText("Touch and Drag one finger");
textView.setOnTouchListener(this);
setContentView(textView);
}
public boolean onTouch(View arg0, MotionEvent arg1) {
builder.setLength(0);
switch(arg1.getAction()){
case MotionEvent.ACTION_DOWN: builder.append("down, ");
break;
case MotionEvent.ACTION_MOVE:
builder.append("move, ");
break;
case MotionEvent.ACTION_UP:
builder.append("up ,");
break;
case MotionEvent.ACTION_CANCEL:
builder.append("cancel, ");
break;
}
builder.append(arg1.getX());
builder.append(", ");
builder.append(arg1.getY());
Log.d("TouchTest",builder.toString());
textView.setText(builder.toString());
return true;
}

}


        單擊屏幕要實現OnTouchListener,實現public boolean onTouch(View arg0, MotionEvent arg1)方法。
MotionEvent有4個Action,分別是 MotionEvent.ACTION_DOWN, ACTION_UP, ACTION_MOVE, ACTION_CANCEL。

        意思分別是觸摸屏按下,松開,移動,和取消。

java代碼:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=http://schemas.android.com/apk/res/android
package="eoe.gamedev"
android:versionCode="1"
android:versionName="1.0">

<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".AndroidBasis"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity android:name=".LifeCycleTest" android:label="LifeCycleTest" android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".SingleTouchTest"
android:label="SingleTouchTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".MultiTouchTest"
android:label="MultiTouchTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".KeyTest"
android:label="KeyTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".AccelerometerTest"
android:label="AccelerometerTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".AssetsTest"
android:label="AssetsTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".ExternalStorageTest"
android:label="ExternalStorageTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".SoundPoolTest"
android:label="SoundPoolTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".MediaPlayerTest"
android:label="MediaPlayerTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".RenderViewTest"
android:label="RenderViewTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".BitmapTest"
android:label="BitmapTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
<activity
android:name=".FontTest"
android:label="FontTest"
android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation">
</activity>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>

</manifest>

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved