我正在使用计步器,它运行顺畅。我想制作一个重置按钮,但它给了我一些问题。
这是我的代码。
public class MainActivity extends Activity implements SensorEventListener {
private TextView textView;
private Button resetButton;
private SensorManager mSensorManager;
private Sensor mStepCounterSensor;
private Sensor mStepDetectorSensor;
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.pedometer);
resetButton = (Button) findViewById(R.id.resetButton);
mSensorManager = (SensorManager)
getSystemService(Context.SENSOR_SERVICE);
mStepCounterSensor = mSensorManager
.getDefaultSensor(Sensor.TYPE_STEP_COUNTER);
mStepDetectorSensor = mSensorManager
.getDefaultSensor(Sensor.TYPE_STEP_DETECTOR);
}
public void onSensorChanged(SensorEvent event) {
Sensor sensor = event.sensor;
float[] values = event.values;
int value = -1;
if (values.length > 0) {
value = (int) values[0];
}
if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) {
textView.setText("Step Counter Detected : " + value);
} else if (sensor.getType() == Sensor.TYPE_STEP_DETECTOR) {
textView.setText("Step Detector Detected : " + value);
}
}
//check this reset
public void reset(SensorEvent event){
Sensor sensor = event.sensor;
float[] values = event.values;
int value = -1;
if (values.length > 0) {
value = 0;
}
if (sensor.getType() == value) {
textView.setText("Step Counter Detected : " + value);
}
}
protected void onResume() {
super.onResume();
mSensorManager.registerListener(this, mStepCounterSensor,
SensorManager.SENSOR_DELAY_FASTEST);
mSensorManager.registerListener(this, mStepDetectorSensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
protected void onStop() {
super.onStop();
mSensorManager.unregisterListener(this, mStepCounterSensor);
mSensorManager.unregisterListener(this, mStepDetectorSensor);
}
}
我正在将重置方法连接到我的按钮,以便在我单击它时。它将我的计步器重置为零。我尝试了不同的方法,但它不适合我。 我也不确定,因为我的老师教我在参数中使用View v。我不知道这与它有什么关系。
答案 0 :(得分:-1)
这段代码不清楚。
MikesDataContext dc = new MikesDataContext()