EditText上的setText无法正常工作

时间:2013-11-20 11:56:56

标签: java android android-edittext

我将一些正确的内容放在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);

        }
    }

3 个答案:

答案 0 :(得分:1)

尝试使用:

        tvX.setText(String.ValueOf(deltaX));
        tvY.setText(String.ValueOf(deltaY));
        tvZ.setText(String.ValueOf(deltaZ));

答案 1 :(得分:1)

试试这个..

你可以用两种方式做到这一点

tvX.setText(""+deltaX);
tvY.setText(""+deltaY);
tvZ.setText(""+deltaZ);

tvX.setText(String.ValueOf(deltaX));
tvY.setText(String.ValueOf(deltaY));
tvZ.setText(String.ValueOf(deltaZ));

答案 2 :(得分:1)

docs说EditText有一个方法setText(),它带有2个args:

setText(CharSequence text, TextView.BufferType type)
  

设置此TextView要显示的文本(请参阅setText(CharSequence)),并设置它是否存储在可设置样式/ spannable的缓冲区中以及是否可编辑。

(TextView.BufferType docs 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);