我正在尝试制作计步器。可以看出,一旦一个特定方向的加速度超过1,计数器就会增加1。然后我想在textView中显示计数。但是,textView 始终显示为“TextView”而不是所需的数字。
任何想法?谢谢!
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements SensorEventListener {
Button startButton;
Button pauseButton;
Button resetButton;
TextView textView;
private SensorManager sensorManager;
private long lastUpdate;
private boolean isCalibrated = false;
private int referrenceAxis;
private int stepCounts = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView1);
// start button
startButton = (Button) findViewById(R.id.button1);
startButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
// pause button
pauseButton = (Button) findViewById(R.id.button2);
pauseButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
// reset button
resetButton = (Button) findViewById(R.id.button3);
resetButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
}
});
init();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void init() {
sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
lastUpdate = System.currentTimeMillis();
}
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() == Sensor.TYPE_LINEAR_ACCELERATION) {
long actualTime = System.currentTimeMillis();
if (isCalibrated && (actualTime - lastUpdate > 500)) {
if (event.values[referrenceAxis] > 1) {
stepCounts++;
textView.setText(String.valueOf(stepCounts));
}
}
lastUpdate = actualTime;
}
if (event.sensor.getType() == Sensor.TYPE_GRAVITY) {
if (!isCalibrated) {
if ((event.values[0] >= event.values[1])&&(event.values[0] >= event.values[2]))
referrenceAxis = 0;
if ((event.values[1] >= event.values[0])&&(event.values[1] >= event.values[2]))
referrenceAxis = 1;
if ((event.values[2] >= event.values[0])&&(event.values[2] >= event.values[1]))
referrenceAxis = 2;
isCalibrated = true;
}
}
}
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
protected void onResume() {
super.onResume();
// register this class as a listener for the sensors
sensorManager
.registerListener(this, sensorManager
.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION),
SensorManager.SENSOR_DELAY_NORMAL);
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY),
SensorManager.SENSOR_DELAY_NORMAL);
}
protected void onPause() {
// unregister listener
super.onPause();
sensorManager.unregisterListener(this);
}
}
XML如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/button1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_centerHorizontal="true"
android:text="@string/button2" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/button3" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/textview" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:text="@string/step_mumber"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>