あなたの質問は少し漠然としています。エラーが発生していますか?パスワード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();
}
}
幸運を!