Android - 在运行时更改textView

时间:2010-09-16 11:14:05

标签: android runtime textview

我正在使用直线布局,我正在尝试更改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>

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

3 个答案:

答案 0 :(得分:1)

尝试在视图上调用invalidate。

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

this.text.invalidate();

这会导致重绘。

抱歉,太快了,我看到你已经尝试过了。 那不行吗?你为什么评论那个?

答案 1 :(得分:1)

您忘了在Toast上调用show()方法:)。试试这个:

Toast.makeText(getBaseContext(), "New text", Toast.LENGTH_SHORT).show();

答案 2 :(得分:0)

尽量不要使用getBaseContext()。使用Position.this

现在你的无限循环是由

引起的

    private void searchPosition(){
        boolean condition = true;
        do{
           determinatePosition();
        }while(condition == true);
    }

尝试将吐司移至public void onClick(View v)

另外,向我们展示您的进口

相关问题