简体   繁体   中英

Is there a way to set a variable's value to a TextView's text using XML in Android Studio?

I am writing a little program for Android (I am a newbie to Java), and I wonder if there's a way to set a TextView-text's value to that of a public static double variable already defined in the corresponding .java file, only by using XML code, like I can do with DynamicResource in C#-XAML.

I have managed to programatically set the TextView's text by using

TextView value1 = (TextView) findViewById(R.id.textView1);
value1.setText(String.valueOf(MyClass.staticVariableInMyClass));

But I would really like to do it, if possible, by editing the XML files of my project, as I do not need to update the TextView's text anymore.

Thanks in advance.

Ways to set text in TextView : XML - add attribute to TextView element android:text="your text" . Java:

TextView tvId = (TextView) findViewById(R.id.tv_id); tvId.setText("your text");

If you have double variable convert it like this. String.valueof(double var);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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