我正在尝试制作一个按钮颜色随接近传感器而变化的应用程序。
我不知道如何改变它onSensorChanged
。我必须导入颜色并实现不同的颜色状态吗?我有点困惑。
这是我的代码:
public class MainProxmityActivity extends Activity implements SensorEventListener {
Button proximidade;
SensorManager Sm;
Sensor ProxSensor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_proxmity);
Sm = (SensorManager) getSystemService (SENSOR_SERVICE);
ProxSensor = Sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
proximidade = (Button) findViewById(R.id.btn_proximidade);
Sm.registerListener(this, ProxSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@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_proxmity, menu);
return true;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
proximidade.setBackgroundColor(getTitleColor("bd4c31"));
}
}
希望问题很清楚。