我使用接近传感器来增加TextView
中的数字,它的工作,但它以两个增量触发0 2 4 6 8
。我希望得到1的增量,可能吗?
TextView text ;
SensorManager sm ;
Sensor Proxsens;
int i=0 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text =(TextView)findViewById(R.id.text_id);
sm = (SensorManager)getSystemService(SENSOR_SERVICE);
Proxsens = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
sm.registerListener(this,Proxsens,SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public void onSensorChanged(SensorEvent event) {
text.setText(String.valueOf(i));
i++;
// text.setText(String.valueOf(event.values[0]));
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event){
if (event.values[0] == 0) {
text.setText(String.valueOf(i));
i++;
}
}