如何从匿名类调用方法

时间:2017-03-26 18:12:41

标签: java android

我有这个课程,可以检测手机正面朝上以及何时面朝下并在屏幕上打印面朝上或面朝下!

package com.example.kyriakos.androiddetectflipping;
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.widget.TextView;
import java.util.List;

public  class Test extends Activity {

    SensorManager sensorManager;
    Sensor accelerometerSensor;
    boolean accelerometerPresent;

    TextView face;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_android_detect_flipping);

        face = (TextView)findViewById(R.id.face);

        sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        List<Sensor> sensorList = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
        if(sensorList.size() > 0){
            accelerometerPresent = true;
            accelerometerSensor = sensorList.get(0);
        }
        else{
            accelerometerPresent = false;
            face.setText("No accelerometer present!");
        }
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if(accelerometerPresent){
            sensorManager.registerListener(accelerometerListener, accelerometerSensor, SensorManager.SENSOR_DELAY_NORMAL);
        }
    }

    @Override
    protected void onStop() {

        super.onStop();
        if(accelerometerPresent){
            sensorManager.unregisterListener(accelerometerListener);
        }
    }

    public SensorEventListener accelerometerListener = new SensorEventListener(){

        @Override
        public void onAccuracyChanged(Sensor arg0, int arg1) {

        }

        @Override
        public void onSensorChanged(SensorEvent arg0) {

            float z_value = arg0.values[2];
            if (z_value >= 0){
                face.setText("Face UP");
            }
            else{
                face.setText("Face DOWN");
            }
        }};


}

现在我想打印另一个类

的消息
package com.example.kyriakos.androiddetectflipping;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class Flip extends AppCompatActivity{


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

}}

它认为onSensorChanged方法负责打印消息,所以我试着在类翻转中调用onSensorChanged方法,就像这样

    SensorEventListener ob = new SensorEventListener();
    ob.onSensorChanged;

但这是错误的,因为SensorEventListener是一个匿名的内部类!!所以我的问题是如何调用这个方法?我读到了如何从一个匿名内部类调用一个方法,但我不太了解它好吧。如果我的问题很愚蠢,但我是java,android的新手,我正在努力学习

1 个答案:

答案 0 :(得分:1)

您&#39;不要将匿名类分配给ob,而是将其基类分配给Test test = new Test(); SensorEventListener ob = test.accelerometerListener; 。如果您想访问它并使用它超载的方法,您必须这样做:

 <header class="header">
  <div class="header__branding">
    <h1>Site branding</h1>
  </div>
  <div class="header__menu">
    <nav class="main-menu">
      <ul class="main-menu list">
        <li class="list__item"><a href="" class="list__item-link">link</a></li>
        <li class="list__item"><a href="" class="list__item-link--active">link</a></li>
        <li class="list__item"><a href="" class="list__item-link">link</a></li>
      </ul>
    </nav>
  </div>
</header>

<footer class="footer">
  <div class="footer__links">
    <ul class="???? list">
      <li class="list__item"><a href="" class="list__item-link">link</a></li>
      <li class="list__item"><a href="" class="list__item-link">link</a></li>
      <li class="list__item"><a href="" class="list__item-link">link</a></li>
    </ul>
  </div>
</footer>

然后你可以调用你的覆盖方法。