編輯: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>
SharedPreferences是Android中最容易理解的數據存儲技術,實際上SharedPreferences處理的就是一個key-value(鍵值對)。
Android對這種方法進行了封裝,我們沒有權限去調用這個方法,所以我們只能通過AIDL,然後利用Java的反射機制去調用系統級的方法。 下面上代碼:(注釋比較詳
學習目的: 1、掌握在Android中如何建立CheckBox 2、掌握CheckBox的常用屬性 3、掌握CheckBox選中狀態變換的事件(監聽器) Chec
本文實例講述了Android編程實現QQ表情的發送和接收。分享給大家供大家參考,具體如下: 在自己做一個聊天應用練習的時候,需要用到表情,於是就想著模仿一下QQ表