gpt4 book ai didi

Android - 在运行时更改 textView

转载 作者:行者123 更新时间:2023-11-29 22:31:55 25 4
gpt4 key购买 nike

我正在使用线性布局,我正在尝试更改 TextView 的文本,但它不会刷新。当我调试时,我检查了文本是否已正确更改。

谢谢

public class Position extends Activity {
Button botonempezar;
Button botonsalir;
TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.position);
botonsalir = (Button) findViewById(R.id.salir);
botonempezar = (Button) findViewById(R.id.Empezar);
text = (TextView) findViewById(R.id.Textoposicion);
botonsalir.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
onDestroy();
finish();
}
});
botonempezar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
searchPosition();
}
});
}
private void searchPosition(){
boolean condition = true;
do{
determinatePosition();
}while(condition == true);
}

private void determinatePosition(){
....
this.text.setText("New text");
//this.text.invalidate();
this.text.postInvalidate();
Toast.makeText(getBaseContext(), "New text", Toast.LENGTH_SHORT);// this doesnt work neither.
....
}

这里贴出xml文件的代码。这真的很愚蠢,但这可能是问题所在:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">


<TextView
android:layout_height="wrap_content"
android:id="@+id/Textoposicion"
android:text="Posición desconocida"
android:layout_width="fill_parent"
android:layout_marginTop="20dip"
android:inputType="text">
</TextView>
<Button android:layout_height="wrap_content"
android:id="@+id/Empezar" android:text="Empezar"
android:layout_width="fill_parent"
android:layout_marginTop="20dip">
</Button>
<Button
android:layout_height="wrap_content"
android:id="@+id/salir"
android:layout_marginTop="20dip"
android:text="Finalizar programa"
android:gravity="center" android:layout_width="fill_parent">
</Button>


</LinearLayout>

我已经编辑了帖子,因为我省略了部分代码以使其更简单,但我想我也可以抑制这个问题。它在无限循环中运行,这可能是错误吗?

最佳答案

尝试在 View 上调用 invalidate。

http://developer.android.com/reference/android/view/View.html#invalidate()

this.text.invalidate();

这将导致重绘。

抱歉,太快了,我看到你已经试过了。那没用吗?你为什么把它评论掉?

关于Android - 在运行时更改 textView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3726083/

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