gpt4 book ai didi

java - EditText 上的 setText 无法正常工作

转载 作者:行者123 更新时间:2023-12-01 23:25:16 26 4
gpt4 key购买 nike

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

Sets the text that this TextView is to display (see setText(CharSequence)) and also sets whether it is stored in a styleable/spannable buffer and whether it is editable.

(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() 方法中,您是否设置内容 View ?

setContentView(R.layout.YOUR_LAYOUT_NAME_WITH_editText1);

关于java - EditText 上的 setText 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20095315/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com