使用其他代码无法同时获取按钮

时间:2011-11-23 13:26:50

标签: java android

我正在尝试为Android编写一个非常简单的应用程序。 它应该做两件事:

  1. TextView中的Dipslay方向传感器信息。
  2. 点击按钮显示祝酒词。
  3. 使用以下代码显示传感器信息,但无法单击按钮:

    package com.example.hello;
    
    import android.app.Activity;
    import android.content.Context;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    
    
    
    public class HelloAndroid extends Activity implements OnClickListener {
        /* Called when the activity is first created. */
        private SensorManager mSensorManager;
        private Sensor mSensor;
        private float[] mValues;
    
        public void onClick(View v) {
            // do something when the button is clicked
            Context context = getApplicationContext();
            Toast toast = Toast.makeText(context, "Hello", Toast.LENGTH_LONG);
            toast.show();
    
        }
    
        private final SensorEventListener mListener = new SensorEventListener() {
            public void onSensorChanged(SensorEvent event) {
                mValues = event.values;
                setContentView(R.layout.main);
                TextView t=(TextView)findViewById(R.id.status); 
                t.setText(Float.toString(mValues[0]) + "\n" +
                          Float.toString(mValues[1]) + "\n" +
                          Float.toString(mValues[2]));
            }
    
            public void onAccuracyChanged(Sensor sensor, int accuracy) {
            }
        };
    
    
        @Override    
        public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
    
            Button button = (Button)findViewById(R.id.start);
            button.setOnClickListener(this);
    
    
            mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
            mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
    
            mSensorManager.registerListener(mListener, mSensor,
                    SensorManager.SENSOR_DELAY_GAME);
    
        }
    
    
    
    }
    

    如果我评论显示传感器信息的部分代码按钮开始工作:

        private final SensorEventListener mListener = new SensorEventListener() {
            public void onSensorChanged(SensorEvent event) {
                mValues = event.values;
          /*    setContentView(R.layout.main);
                TextView t=(TextView)findViewById(R.id.status); 
                t.setText(Float.toString(mValues[0]) + "\n" +
                          Float.toString(mValues[1]) + "\n" +
                          Float.toString(mValues[2]));
              */ }
    

    我做错了什么?如何让两件事同时发挥作用?

2 个答案:

答案 0 :(得分:4)

也许您可以尝试仅在onCreate上设置上下文视图。不要在SensorEvent侦听器上再次设置它。

答案 1 :(得分:3)

只需从方法setContentView(R.layout.main)中删除行onSensorChanged,它就会重复,因为您的setContentView()方法上已经存在方法onCreate(), 如果有问题请告诉我