你的问题有点含糊。你有任何错误吗?我看到你在我猜是密码 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();
}
}
祝你好运!