你的問題有點含糊。你有什麼錯誤嗎?我看到你正在使用一個動畫我猜的是密碼TextView,但我沒有看到任何加速度計分析。無論如何,你的代碼應該工作,假設你有一個叫path
的類。命名約定表明類應以大寫字母開頭(如Activity,Animation,Button),所以我將您的類重命名爲Path
。它應該是這樣的:
public class Path extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.path);
...
}
...
}
現在您的Manifest.xml文件,你需要這樣的:
<application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name=".mainactivity"
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=".Path" /> <!-- Add me! -->
</application>
最後我要作一個假設,loginButton
是一個按鈕,而不是一般的景觀。在這種情況下,您的loginButton
代碼應該是這樣的:
Button loginButton = (Button) findViewById(R.id.login);
loginButton.setOnClickListener(this);
希望幫助,祝你好運!
加成
下面是如何實現識別通用搖運動的樣本:
public class Example extends Activity implements SensorEventListener {
private float mAccCurrent;
private float mAccLast;
private SensorManager mSensorManager;
private int mShakeCount = 0;
private TextView mText;
// Sensor events
public void onSensorChanged(SensorEvent event) {
mAccLast = mAccCurrent;
mAccCurrent = (float) Math.sqrt((Math.pow(event.values[0], 2) + Math.pow(event.values[1], 2) + Math.pow(event.values[2], 2)));
float acceleration = mAccCurrent - mAccLast;
mText.setText(acceleration + "");
if(Math.abs(acceleration) > 2) {
Toast.makeText(this, "Shake " + mShakeCount++, 1).show();
// Navigate here
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {}
// Activity events
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mText = (TextView) findViewById(R.id.text);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
mAccCurrent = SensorManager.GRAVITY_EARTH;
}
@Override
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onStop() {
mSensorManager.unregisterListener(this);
super.onStop();
}
}
祝您好運!
來源
2012-06-18 17:32:17
Sam
沒錯這就是工作在按鈕click.i需要給1個搖用於導航到另一個page.so這就是爲什麼我使用shake.if用戶搖動1,則頁面應根據晃動導航.. –
你有什麼試過?如果您想了解如何使用加速度計的示例,請參閱[加速度計播放](http://developer.android.com/resources/samples/AccelerometerPlay/src/com/example/android/accelerometerplay/AccelerometerPlayActivity.html) Android SDK。 – Sam
ShakeEvent mSensorListener;得到錯誤。所以我不能做到這一點http://pastebin.com/TvUVv0Eh –