1
    package com.andineagoe.gradienttermic;

    import android.app.Activity;
    import android.hardware.Sensor;
    import android.hardware.SensorEvent;
    import android.hardware.SensorEventListener;
    import android.hardware.SensorManager;
    import android.os.Bundle;
    import android.os.SystemClock;
    import android.view.KeyEvent;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;

    public class PressureAltimeterActivity extends Activity implements SensorEventListener {

        private PressureAltimeterActivity this_activity_;
        protected Sensor sensor_pressure_;
        protected SensorManager sensor_manager_;
        protected double slp_inHg_;
        protected double pressure_hPa_;
        protected KalmanFilter pressure_hPa_filter_;
        protected double last_measurement_time_;

        private static final double SLT_K = 288.15; 
        private static final double TLAPSE_K_PER_M = -0.0065; 
        private static final double G_M_PER_S_PER_S = 9.80665;
        private static final double R_J_PER_KG_PER_K = 287.052; 
        private static final double PA_PER_INHG = 3386; 
        private static final double FT_PER_M = 3.2808399;  
        private static final double KF_VAR_ACCEL = 0.0075; 
        private static final double KF_VAR_MEASUREMENT = 0.05;

在这里我尝试添加我的按钮和文本视图,它可以工作

        **Button mButton = (Button) findViewById(R.id.saveButton);
        TextView mPressure = (TextView) findViewById(R.id.pressureValue);
        TextView mTemperature = (TextView) findViewById(R.id.tempValue);
        TextView mLogSave = (TextView) findViewById(R.id.logSave);

现在,当我尝试在此行设置 onClick 侦听器多个标记时出现 3-4 错误 - 令牌“setOnClickListener”上的语法错误,= 此令牌之后的预期 - 令牌上的语法错误,错位的构造

PressureAltmeterActivity 类型的方法 onClick(View) 必须重写或实现超类型方法语法错误,插入“}”完成 ClassBody

        mButton.setOnClickListener(new View.onClickListner(){

            @Override
            public void onClick(View v){

            //do stuff

            });

        public void saveData (){

        }**

我只想在每次按下按钮时修改一些日志,但我似乎无法这样做

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            pressure_hPa_filter_ = new KalmanFilter(KF_VAR_ACCEL);

            setContentView(R.layout.main);

            sensor_manager_ = (SensorManager) getSystemService(SENSOR_SERVICE);
            sensor_pressure_ = sensor_manager_.getDefaultSensor(Sensor.TYPE_PRESSURE);

        }

        @Override
        public void onResume() {
            super.onResume();

            this_activity_ = this;

            if (slp_inHg_ < 28.1 || slp_inHg_ > 31.0) slp_inHg_ = 29.92;

            pressure_hPa_ = 1013.0912;
            pressure_hPa_filter_.reset(pressure_hPa_);
            last_measurement_time_ = SystemClock.elapsedRealtime() / 1000.;

            if (sensor_pressure_ != null) {
                sensor_manager_.registerListener(this, sensor_pressure_, SensorManager.SENSOR_DELAY_GAME);
            } 
       }


        @Override
        public void onPause() {
            super.onPause();
            sensor_manager_.unregisterListener(this);
        }

        @Override
        public void onDestroy() {
            super.onDestroy();
        }

        protected void onSaveInstanceState(Bundle bundle) {
            super.onSaveInstanceState(bundle);
            bundle.putDouble("slp_inHg", slp_inHg_);
        }

        protected void onRestoreInstanceState(Bundle bundle) {
            super.onRestoreInstanceState(bundle);
            slp_inHg_ = bundle.getDouble("slp_inHg");
        }

        public boolean onKeyDown(int key_code, KeyEvent key_event) {
            if (key_code != KeyEvent.KEYCODE_VOLUME_UP &&
                key_code != KeyEvent.KEYCODE_VOLUME_DOWN) return false;

            long slp_inHg_long = Math.round(100.0 * slp_inHg_);
            if (key_code == KeyEvent.KEYCODE_VOLUME_UP) {
                if (slp_inHg_long < 4031) ++slp_inHg_long;
            } else if (key_code == KeyEvent.KEYCODE_VOLUME_DOWN) {
                if (slp_inHg_long > 2028) --slp_inHg_long;
            }
            slp_inHg_ = slp_inHg_long / 100.0;

            return true;
        }

        public boolean onKeyUp(int key_code, KeyEvent key_event) {
            return key_code == KeyEvent.KEYCODE_VOLUME_UP ||
                   key_code == KeyEvent.KEYCODE_VOLUME_DOWN;
        }


        public void onAccuracyChanged(Sensor arg0, int arg1) {
        }

        public void onSensorChanged(SensorEvent event) {

            if (event.sensor.getType() != Sensor.TYPE_PRESSURE) return;  // Should not occur.
            pressure_hPa_ = event.values[0];

            final double curr_measurement_time = SystemClock.elapsedRealtime() / 1000.;
            final double dt = curr_measurement_time - last_measurement_time_;
            pressure_hPa_filter_.update(pressure_hPa_, KF_VAR_MEASUREMENT, dt);
            last_measurement_time_ = curr_measurement_time;

            double inaltime = (long)hPaToFeet(slp_inHg_, pressure_hPa_filter_.getXAbs());
            TextView textView = (TextView) findViewById(R.id.pressureValue);
            textView.setText("" + inaltime);
        }

        private static double hPaToFeet(double slp_inHg, double pressure_hPa) {
            double factor_m = SLT_K / TLAPSE_K_PER_M;
            double exponent = -TLAPSE_K_PER_M * R_J_PER_KG_PER_K / G_M_PER_S_PER_S;     
            double current_sea_level_pressure_Pa = slp_inHg * PA_PER_INHG;
            double altitude_m =
                    factor_m *
                    (Math.pow(100.0 * pressure_hPa / current_sea_level_pressure_Pa, exponent) - 1.0);
            return /**FT_PER_M* */  altitude_m;
        }

    }
4

1 回答 1

1

尝试实现OnClickListener你的类和方法onClick

public class LoginActivity extends Activity implements OnClickListener {

 //...
@Override
    public void onClick(View view) {
        switch (view.getId()) {
        case mButton.getId():
            //implement action
            break;

        default:
            break;
        }
    }
}
于 2013-11-08T19:59:02.757 回答