-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

公共类 ACTIVITY 扩展 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 回答