[英]setText on EditText is not working properly
將正確的內容放入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);
}
}
嘗試使用:
tvX.setText(String.ValueOf(deltaX));
tvY.setText(String.ValueOf(deltaY));
tvZ.setText(String.ValueOf(deltaZ));
嘗試這個..
你可以用兩種方式做到這一點
tvX.setText(""+deltaX);
tvY.setText(""+deltaY);
tvZ.setText(""+deltaZ);
要么
tvX.setText(String.ValueOf(deltaX));
tvY.setText(String.ValueOf(deltaY));
tvZ.setText(String.ValueOf(deltaZ));
文檔說EditText有一個setText()方法,它需要2個參數:
setText(CharSequence text, TextView.BufferType type)
設置此TextView要顯示的文本(請參見setText(CharSequence)),還設置是否將其存儲在可樣式化/可擴展的緩沖區中以及是否可編輯。
(TextView.BufferType文檔在這里 )
因此,在您的代碼中,您正在執行以下操作:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.