-3

電話のシェイクを使用してページ間を移動するアプリケーションを開発したいと考えています。私のアプリのメイン ページには 2 つのボタンがあります。この基本的なコードを使用しましたが、ページ ナビゲーションがどのように行われるのか理解できません。解決策を教えてください。

public class mainactivity extends Activity implements View.OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        View loginButton = findViewById(R.id.login);
        loginButton.setOnClickListener(this);
    }

    public void onClick(View v) {
        Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake);
        findViewById(R.id.pw).startAnimation(shake);
        // Toast.makeText(this, "Wrong Password", Toast.LENGTH_SHORT).show();
        Intent my = new Intent(getApplicationContext(), path.class);
        startActivityForResult(my, 0);
    }
}
4

2 に答える 2

0

あなたの質問は少し漠然としています。エラーが発生していますか?パスワード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-18T17:32:17.627 に答える
0

public class ACTIVITY extends Activity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            SensorManager mSensorManager;

            ShakeEvent mSensorListener;

            mSensorListener = new ShakeEvent();
            mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
            mSensorManager.registerListener(mSensorListener,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_UI);


            mSensorListener.setOnShakeListener(new ShakeEvent.OnShakeListener() {

              public void onShake() {
                  Intent i = new Intent(Shake.this, NEWACTIVITY.class);
                  startActivity(i);
              }
            });
        }}
于 2012-06-27T03:53:56.473 に答える