简体   繁体   English

如何在 Java 代码中更改 TextView 值?

[英]How do I change TextView Value inside Java Code?

I am working on a android program.我正在开发一个 android 程序。 A user clicks on a button I do some math and I would like to change the values that I have on my view in some TextView objects.用户单击一个按钮,我做了一些数学运算,我想更改某些 TextView 对象中视图上的值。 Can someone please tell me how to do it in my code?有人可以告诉我如何在我的代码中做到这一点吗?

I presume that this question is a continuation of this one .我相信,这个问题的延续这一个

What are you trying to do?你想做什么? Do you really want to dynamically change the text in your TextView objects when the user clicks a button?当用户单击按钮时,您真的想动态更改 TextView 对象中的文本吗? You can certainly do that, if you have a reason, but, if the text is static, it is usually set in the main.xml file, like this:你当然可以这样做,如果你有理由,但是,如果文本是静态的,它通常在 main.xml 文件中设置,如下所示:

<TextView  
android:id="@+id/rate"
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:text="@string/rate"
/>

The string "@string/rate" refers to an entry in your strings.xml file that looks like this:字符串“@string/rate”指的是 strings.xml 文件中的一个条目,如下所示:

<string name="rate">Rate</string>

If you really want to change this text later, you can do so by using Nikolay's example - you'd get a reference to the TextView by utilizing the id defined for it within main.xml, like this:如果您以后真的想更改此文本,可以使用 Nikolay 的示例来进行更改 - 您可以使用 main.xml 中为其定义的 id 来获得对 TextView 的引用,如下所示:


final TextView textViewToChange = (TextView) findViewById(R.id.rate);
textViewToChange.setText(
    "The new text that I'd like to display now that the user has pushed a button.");

First we need to find a Button :首先我们需要找到一个Button

Button mButton = (Button) findViewById(R.id.my_button);

After that, you must implement View.OnClickListener and there you should find the TextView and execute the method setText :之后,您必须实现View.OnClickListener并在那里找到TextView并执行方法setText

mButton.setOnClickListener(new View.OnClickListener {
    public void onClick(View v) {
        final TextView mTextView = (TextView) findViewById(R.id.my_text_view);
        mTextView.setText("Some Text");
    }
});

First, add a textView in the XML file首先在xml文件中添加一个textView

<TextView  
    android:id="@+id/rate_id"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/what_U_want_to_display_in_first_time"
    />

then add a button in xml file with id btn_change_textView and write this two line of code in onCreate() method of activity然后在xml文件中添加一个id为btn_change_textView的按钮,并在活动的onCreate()方法中写入这两行代码

Button btn= (Button) findViewById(R.id. btn_change_textView);
TextView textView=(TextView)findViewById(R.id.rate_id);

then use clickListener() on button object like this然后像这样在按钮对象上使用clickListener()

btn.setOnClickListener(new View.OnClickListener {
    public void onClick(View v) {
      
        textView.setText("write here what u want to display after button click in string");
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM