我有一个简单的Activity
调用SingleTouchTest
来理解屏幕触摸。奇怪的是,它SingleTouchTest
以我所处的任何方向开始,但旋转设备不会导致屏幕旋转。
我的测试设备是运行 Android 4.0.3 的 Acer A100。
mainActivity
包含一个ListView
导航到我的测试Activity
es,包括SingleTouchTest
. SingleTouchTest
除了旋转之外,我可以毫无问题地运行(下面的完整代码)。在AndroidManifest.xml
(下面的完整代码)中,我尝试了每种组合
android:configChanges="keyboard|keyboardHidden|orientation"
android:screenOrientation="unspecified"
它不会自动旋转。我什至删除了该onConfigurationChanged()
方法SingleTouchTest
,但没有任何反应。
完整代码AndroidManifest.xml
:
<?xml version="1.0" encoding="utf-8"?>
<manifest
package="com.Bespoke.AndroidBasics"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="preferExternal" xmlns:android="http://schemas.android.com/apk/res/android">
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:icon="@drawable/ic_launcher"
android:label="Android Basics">
<activity
android:name=".AndroidBasicsStarter"
android:label="Android Basics"
android:screenOrientation="unspecified">
<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="Life Cycle Test"
android:configChanges="keyboard|keyboardHidden|orientation" android:screenOrientation="unspecified"/>
<activity
android:name=".SingleTouchTest"
android:label="Single Touch Test"
android:configChanges="keyboard|keyboardHidden|orientation" android:screenOrientation="unspecified"/>
<activity
android:name=".MultiTouchTest"
android:label="Single Touch Test"
android:configChanges="keyboard|keyboardHidden|orientation" android:screenOrientation="unspecified"/>
<activity
android:name=".KeyTest"
android:label="Key Test" android:screenOrientation="unspecified"/>
</application>
</manifest>
完整代码SingleTouchTest
:
package com.Bespoke.AndroidBasics;
import android.app.Activity;
import android.content.res.Configuration;
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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
textView = new TextView(this);
textView.setText("Touch and drag (one finger only!)");
textView.setOnTouchListener(this);
this.setContentView(textView);
}
public boolean onTouch(View v, MotionEvent event) {
builder.setLength(0); // clear the builder
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
builder.append("down, ");
break;
case MotionEvent.ACTION_MOVE:
builder.append("move, ");
break;
case MotionEvent.ACTION_CANCEL:
builder.append("cancel, ");
break;
case MotionEvent.ACTION_UP:
builder.append("up, ");
break;
}
builder.append(event.getX());
builder.append(", ");
builder.append(event.getY());
String text = builder.toString();
Log.d("TouchTest", text);
textView.setText(text);
return true; // Consume the event, if false super.onTouch superceeds us
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
}