Android - 在运行时更改textView

发布于 2024-09-19 20:50:07 字数 2591 浏览 6 评论 0原文

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

我编辑了这篇文章,因为我省略了部分代码以使其更简单,但我认为我也可能抑制了这个问题。它无限循环地运行,这可能是错误吗?

I´m working with a lineal layout and I´m trying to change the text of a TextView but it doesn´t refresh. When I debug I´ve checked that the text have changed correctly.

Thanks

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.
         ....
    }

Here I post the code of the xml file. Its really silly but it could be the problem:

<?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>

I have edited the post because I omitted part of the code to make it simpler but I think I may suppressed the problem too. It runs in a endless loop, could be this the mistake?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤城病女 2024-09-26 20:50:07

尝试对视图调用 invalidate。

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

this.text.invalidate();

这将导致重绘。

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

Try to call invalidate on the view.

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

this.text.invalidate();

This will cause a redraw.

Sorry, too quick, I saw that you already tried that.
Didn't that work? Why did you comment that away?

卸妝后依然美 2024-09-26 20:50:07

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

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

You forgot to call show() method on Toast :). Try this:

Toast.makeText(getBaseContext(), "New text", Toast.LENGTH_SHORT).show();
北风几吹夏 2024-09-26 20:50:07

尝试不使用 getBaseContext()。使用 Position.this

现在你的无限循环是由


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

尝试将 toast 移入 public void onClick(View v)

另外,向我们展示你的导入

Try not using getBaseContext(). Use Position.this

Well now your infinite loop is caused by


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

Try moving the toast into public void onClick(View v)

Also, show us your imports

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文