2010-09-16 156 views
1

我正在使用線性佈局,並且正在嘗試更改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> 

我已編輯職位,因爲我省略部分代碼,使其更簡單,但我想我可能會抑制的問題太多。它無限循環運行,可能是這個錯誤?

回答

0

請勿使用getBaseContext()。使用Position.this

現在好了你的無限循環由


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

造成嘗試敬酒遷入public void onClick(View v)

此外,我們展示您的進口

+0

我試過了,它也沒有工作。任何其他想法?它可能與position.xml文件有關? – Miguel 2010-09-17 09:43:05

1

你忘了打電話給show()方法上吐司: )。試試這個:

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