简体   繁体   English

使用Android接近传感器时禁用OnClickListener

[英]OnClickListener disabled when using Android proximity sensor

I am trying to create a proximity sensor in an application that is already working fine. 我正在尝试在已经正常运行的应用程序中创建一个接近传感器。 I am able to run ok the sensor, but when I do it, it disables the OnClickListener of the buttons of the application and I really don't understand why. 我可以正常运行传感器,但是当我这样做时,它禁用了应用程序按钮的OnClickListener,我真的不明白为什么。

This is my MainActivity.class 这是我的MainActivity.class

public class MainActivity extends Activity implements OnClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button option1 = (Button) findViewById(R.id.option1);
        Button option2 = (Button) findViewById(R.id.option2);

        option1.setOnClickListener(this);
        option2.setOnClickListener(this);
   }

    @Override
    protected void onResume() {
        super.onResume();       
        Intent intent = new Intent();
        intent.setClass(getBaseContext(),ProximitySensor.class);
        startActivity(intent);      
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.option1:
                // Option1
               break;
            case R.id.option2:
                // Option1
                break;
            default:
                break;
        }

    }
}

And here the ProximitySensor.class that is working OK 这里的ProximitySensor.class工作正常

public class ProximitySensor extends Activity implements SensorEventListener{

    private SensorManager sm;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        sm= (SensorManager)getSystemService(Context.SENSOR_SERVICE);
    }

    @Override
    protected void onStart(){
        super.onStart();
        Sensor proximitySensor= sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
        if (proximitySensor == null){
            Toast.makeText(ProximitySensor.this,"No Proximity Sensor Found! ",Toast.LENGTH_LONG).show();
        }   
    }

    @Override
    protected void onResume() {
        super.onResume();
        sm.registerListener(this, sm.getDefaultSensor(Sensor.TYPE_PROXIMITY),SensorManager.SENSOR_DELAY_NORMAL);
    }

    @Override
    protected void onStop() {
        super.onStop();
        sm.unregisterListener(this);
   }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        if(event.sensor.getType()==Sensor.TYPE_PROXIMITY){
            if(event.values[0] == 0){
               Toast.makeText(ProximitySensor.this,"You are close",Toast.LENGTH_LONG).show();
            }
        }
    }

}

I think that I might be calling wrong the sensor, could anyone give me a hand? 我认为我可能是在打错传感器,有人可以帮我吗?

Many thanks! 非常感谢!

You need to register onClick listeners in ProximitySensor activity just like you did on your main activity. 您需要像在主要活动中一样在ProximitySensor活动中注册onClick侦听器。

You are currently starting a new activity, setting its layout the same as your main activity. 您当前正在开始一个新活动,将其布局设置为与主要活动相同。 But when you do that, the onClickListeners you set up on your main activity no longer works, because it is a new activity and a new layout. 但是当您这样做时,您在主要活动上设置的onClickListeners将不再起作用,因为它是一个新活动和新布局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM