我想开发一个使用手机摇动浏览页面的应用程序。我的应用程序主页面有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 :(得分: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
是一个Button,而不是一般的View。在这种情况下,您的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();
}
}
祝你好运!
答案 1 :(得分:0)
公共类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);
}
});
}}