将正确的内容放入EditText(在此应用程序中为“陀螺仪”)时,我遇到一些问题。我试图在手机上显示陀螺仪的值,直到可以在控制台中成功打印出。

当我使用gyro.setText(string)时,问题就开始了,它什么也没做。我唯一看到的是0.0、0.0、0.0,而我在控制台中得到了正确的数字。某些内容会覆盖当前值吗?还是我在这里遗漏了一些内容?

@Override
    public void onSensorChanged(SensorEvent event) {
        EditText gyro = (EditText)findViewById(R.id.editText1);
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];

        if (!mInitialized) {
            mLastX = x;
            mLastY = y;
            mLastZ = z;
            tvX.setText("0.0");
            tvY.setText("0.0");
            tvZ.setText("0.0");
            gyro.setText("nog niet gestart");
            mInitialized = true;
        } else {
            float deltaX = Math.abs(mLastX - x);
            float deltaY = Math.abs(mLastY - y);
            float deltaZ = Math.abs(mLastZ - z);
            if (deltaX < NOISE) deltaX = (float)0.0;
            if (deltaY < NOISE) deltaY = (float)0.0;
            if (deltaZ < NOISE) deltaZ = (float)0.0;
            mLastX = x;
            mLastY = y;
            mLastZ = z;
            tvX.setText(Float.toString(deltaX));
            tvY.setText(Float.toString(deltaY));
            tvZ.setText(Float.toString(deltaZ));
            iv.setVisibility(View.VISIBLE);
            if (deltaX > deltaY) {
                iv.setImageResource(R.drawable.horizontal);
            } else if (deltaY > deltaX) {
                iv.setImageResource(R.drawable.vertical);
            } else {
                iv.setVisibility(View.INVISIBLE);
            }

            if (timestamp != 0) {
                final float dT = (event.timestamp - timestamp) * NS2S;
                // Axis of the rotation sample, not normalized yet.
                float axisX = event.values[0];
                float axisY = event.values[1];
                float axisZ = event.values[2];

                if (omegaMagnitude > EPSILON) {
                  axisX /= omegaMagnitude;
                  axisY /= omegaMagnitude;
                  axisZ /= omegaMagnitude;
                }

                float thetaOverTwo = omegaMagnitude * dT / 2.0f;
                float sinThetaOverTwo = (float) Math.sin(thetaOverTwo);
                float cosThetaOverTwo = (float) Math.cos(thetaOverTwo);
                deltaRotationVector[0] = sinThetaOverTwo * axisX;
                deltaRotationVector[1] = sinThetaOverTwo * axisY;
                deltaRotationVector[2] = sinThetaOverTwo * axisZ;
                deltaRotationVector[3] = cosThetaOverTwo;

                String latestDeltaRotationVector = Double.toString(deltaRotationVector[0]*0.000277777778)+", "+Double.toString(deltaRotationVector[1]*0.000277777778)+", "+Double.toString(deltaRotationVector[2]*0.000277777778)+", ";
                System.out.println(latestDeltaRotationVector);
                gyro.setText(latestDeltaRotationVector);

            }
              timestamp = event.timestamp;
              float[] deltaRotationMatrix = new float[9];
              SensorManager.getRotationMatrixFromVector(deltaRotationMatrix, deltaRotationVector);

        }
    }

最佳答案

docs说EditText有一个setText()方法,它需要2个参数:

setText(CharSequence text, TextView.BufferType type)



  设置此TextView要显示的文本(请参见setText(CharSequence)),还设置是否将其存储在可样式化/可扩展的缓冲区中,以及是否可编辑。


(TextView.BufferType文档here

因此,在您的代码中,您正在执行以下操作:

String latestDeltaRotationVector = Double.toString(deltaRotationVector[0]*0.000277777778)+", "+Double.toString(deltaRotationVector[1]*0.000277777778)+", "+Double.toString(deltaRotationVector[2]*0.000277777778)+", ";

System.out.println(latestDeltaRotationVector);

gyro.setText(latestDeltaRotationVector);


尝试通过以下方式设置文本:

gyro.setText(latestDeltaRotationVector, TextView.BufferType.EDITABLE);


要么

gyro.setText(latestDeltaRotationVector, TextView.BufferType.NORMAL);


另外,在您的onCreate()方法中,您是否要设置内容视图?

setContentView(R.layout.YOUR_LAYOUT_NAME_WITH_editText1);

07-25 22:19
查看更多