1

我是 Java 和 Android 编程语言的新手,这是我研究的第一个平台。我想问一下,这个方法怎么访问event.values[1]?

public void onSensorChanged(SensorEvent event) {synchronized (this){
    if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER)

        return;
    mAccVals.x = (float) (event.values[1] * FILTERING_FACTOR + mAccVals.x * (1.0 - FILTERING_FACTOR));
    //mAccVals.y = (float) ((-event.values[1] * FILTERING_FACTOR + mAccVals.y * (1.0 - FILTERING_FACTOR)));
    mAccVals.z = (float) (event.values[2] * FILTERING_FACTOR + mAccVals.z * (1.0 - FILTERING_FACTOR));

    scene.camera().position.x = mAccVals.x * .2f;
    scene.camera().position.z = mAccVals.z * .8f;

    scene.camera().target.x = -scene.camera().position.x;
    scene.camera().target.z = -scene.camera().position.z;

}

我想获取 event.values[1] 然后在 textview 上显示它

protected void onCreateSetContentView()
{   
    setContentView(R.layout.custom_layout_example); 
        LinearLayout ll = (LinearLayout) this.findViewById(R.id.scene1Holder);
        ll.addView(_glSurfaceView);
        TextView myTextView = (TextView) findViewById(R.id.splashTitle); 
        myTextView.setText("Test " + event.values[1]  );
        return;                  
 }

有什么建议我该如何解决这个问题?提前致谢

4

1 回答 1

1

在您中创建一个字段Activity并将其分配给您的R.id.splashTitle

TextView mTextView;
...
protected void onCreateSetContentView() {
    mTextView = (TextView) findViewById(R.id.splashTitle);
    ...
}

然后将其文本设置onSensorChanged()如下:

public void onSensorChanged(SensorEvent event) {
    mTextView.setText("Test " + event.values[1]);
    ...
}

或者您可以将最近的传感器值存储在一个字段中:

// Class field
float[] mSensorValues;
// In onSensorChanged()
mSensorValues = event.values;
// In onCreateSetContentView()
myTextView.setText("Test " + mSensorValues[1]);

为了保留正确的顺序,onCreateonSensorChanged可以使用文档中描述的方法:

protected void onResume() {
     super.onResume();
     mSensorManager.registerListener(this, mAccelerometer,
                                     SensorManager.SENSOR_DELAY_NORMAL);
 }

 protected void onPause() {
     super.onPause();
     mSensorManager.unregisterListener(this);
 }
于 2012-06-12T12:48:13.403 回答