2012-06-18 95 views
-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); 
    } 
} 

回答

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(); 
     } 
} 

祝您好運!

+0

沒錯這就是工作在按鈕click.i需要給1個搖用於導航到另一個page.so這就是爲什麼我使用shake.if用戶搖動1,則頁面應根據晃動導航.. –

+0

你有什麼試過?如果您想了解如何使用加速度計的示例,請參閱[加速度計播放](http://developer.android.com/resources/samples/AccelerometerPlay/src/com/example/android/accelerometerplay/AccelerometerPlayActivity.html) Android SDK。 – Sam

+0

ShakeEvent mSensorListener;得到錯誤。所以我不能做到這一點http://pastebin.com/TvUVv0Eh –

0

公共類活動來延長活動{

 @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); 
       } 
      }); 
     }} 
+0

,但在使用ShakeEvent mSensorListener時會出錯;但這有助於執行頁面導航的基本步驟。如果任何人有其他想法,請將其發佈。 – samadi