Android - 在运行时更改textView
问题描述:
我正在使用线性布局,并且正在尝试更改TextView的文本,但它不会刷新。当我调试时,我检查了文字已经改变了。Android - 在运行时更改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>
我已编辑职位,因为我省略部分代码,使其更简单,但我想我可能会抑制的问题太多。它无限循环运行,可能是这个错误?
答
尝试在视图上调用invalidate。
http://developer.android.com/reference/android/view/View.html#invalidate()
this.text.invalidate();
这将导致重绘。
对不起,太快了,我看到你已经试过了。 没有工作?你为什么对此置评?
答
请勿使用getBaseContext()。使用Position.this
现在好了你的无限循环由
private void searchPosition(){
boolean condition = true;
do{
determinatePosition();
}while(condition == true);
}
造成尝试敬酒迁入public void onClick(View v)
此外,我们展示您的进口
答
你忘了打电话给show()
方法上吐司: )。试试这个:
Toast.makeText(getBaseContext(), "New text", Toast.LENGTH_SHORT).show();
我试过了,它也没有工作。任何其他想法?它可能与position.xml文件有关? – Miguel 2010-09-17 09:43:05