如何从子类中设置titleBar文本

时间:2012-02-19 17:05:00

标签: android android-context android-titlebar

我有一个Main活动,它将两个类加载到FrameLayout视图中。

    //Create Intance of Camera
        camPreview = new CamLayer(this.getApplicationContext());

        //Create Instance of OpenGL
        glView = new GLLayer(this);

        //FrameLayOut for holding everything
        FrameLayout frame = new FrameLayout(this);
        // set as main view
        setContentView(frame);

        // add Camera to view 
        frame.addView(camPreview, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

        frame.addView(glView);

从glView我启动我的传感器管理器,名为PhoneOrientation,如下所示:

    public GLLayer(Context context) {
    super(context);

    this.context = context;
    this.square = new Square();
    phoneOri=new PhoneOrientation(context); // sensor manager and interpreter

    // settings for translucent glView
    this.setEGLConfigChooser(8, 8, 8, 8, 16, 0);
    this.getHolder().setFormat(PixelFormat.TRANSLUCENT);

    // set render to inline 
    this.setRenderer(this);
    phoneOri.start(context);

}

我的phoneOrientation类:

    public class PhoneOrientation {
private SensorManager sensorMan;
private Sensor sensorAcce;
private Sensor sensorMagn;
private SensorEventListener listener;
private float matrix[]=new float[16];
private Context ctx;

public PhoneOrientation(Context context) {
    ctx = context;
}

public void start(Context context) {
    listener = new SensorEventListener() {
        private float orientation[]=new float[3];
        private float acceleration[]=new float[3];

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

        public void onSensorChanged(SensorEvent evt) {
            int type=evt.sensor.getType();

            //Smoothing the sensor data a bit seems like a good idea.
            if (type == Sensor.TYPE_MAGNETIC_FIELD) {
                orientation[0]=(orientation[0]*1+evt.values[0])*0.5f;
                orientation[1]=(orientation[1]*1+evt.values[1])*0.5f;
                orientation[2]=(orientation[2]*1+evt.values[2])*0.5f;
            } else if (type == Sensor.TYPE_ACCELEROMETER) {
                acceleration[0]=(acceleration[0]*2+evt.values[0])*0.33334f;
                acceleration[1]=(acceleration[1]*2+evt.values[1])*0.33334f;
                acceleration[2]=(acceleration[2]*2+evt.values[2])*0.33334f;
            }
            if ((type==Sensor.TYPE_MAGNETIC_FIELD) || (type==Sensor.TYPE_ACCELEROMETER)) {
                float newMat[]=new float[16];
                //Toast toast = Toast.makeText(ctx.getApplicationContext(), "accel", Toast.LENGTH_SHORT);
                //toast.show();
                SensorManager.getRotationMatrix(newMat, null, acceleration, orientation);
                SensorManager.remapCoordinateSystem(newMat,
                        SensorManager.AXIS_Y, SensorManager.AXIS_MINUS_X,
                        newMat);
                matrix=newMat;
            }
        }
    };

    sensorMan = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
    sensorAcce = sensorMan.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
    sensorMagn = sensorMan.getSensorList(Sensor.TYPE_MAGNETIC_FIELD).get(0);

    sensorMan.registerListener(listener, sensorAcce, SensorManager.SENSOR_DELAY_FASTEST);
    sensorMan.registerListener(listener, sensorMagn, SensorManager.SENSOR_DELAY_FASTEST);       
}

public float[] getMatrix() {
    return matrix;
}

public void finish() {
    sensorMan.unregisterListener(listener);
}

}

我的phoniOrientationClass本质上是我的传感器管理器,从那里我想把一些传感器数据吐回到我的应用程序的标题栏,即。

    context.setTitle("x: "+ xData); // or something?  

我似乎不能从那里拨打这个电话吗?我是java的新手,我看到很多人在教程/示例中内联他们的类。我个人不喜欢这么做,因为我相信它会使代码变得混乱。但我想,因为我将我的应用程序上下文从我的主要活动传递到我的glView,然后再次传递给我的传感器管理器,我能够进行此调用吗?谁能向我解释我怎么能?

EL

1 个答案:

答案 0 :(得分:1)

方法下的“大红线”表示:“方法setTitle(String)未定义类型Context”。幸运的是,通过将上下文转换为Activity,可以非常快速地修复此问题。

Activity activity = (Activity) context;
activity.setTitle("x: "+ xData);