简体   繁体   中英

Android App: How can I print out a String with java code?

I am very new to Android programming!

I want to do something really simple:

if(i < 1) 
    System.out.println("Ja");
 else
    System.out.println("Nein");

My first idea how to achieve this was to have a TextView in xml with a String resource:

<TextView
android:id="@+id/mytextview"
android:text="@string/Entscheidung"
android:layout_width="match_parent"
android:layout_height="match_parent" />

and then acess that resource via Java code:

R.string.Entscheidung = "Ja";

but it doesnt work: Error:(13, 17) error: cannot assign a value to final variable Entscheidung

Is there another way to do this?

Thank you !

Firstly I'd just like to clarify the concept of String Resources and how they are used. For constant String values ("Ja" and "Nein" in your case), you can define these values in a file called strings.xml , located within the res/values folder. As an example:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="antwort_ja">Ja</string>
    <string name="antwort_nein">Nein</string>
</resources>

These values are finalized at compile time so can be used as constants throughout your app. In the case of XML layout files, yes you can refer to these String resources using the @string/ prefix. However, in your Java code you can only refer to these values using the getString() method using the R.string prefix. Because these R.string values are final , they cannot be changed at runtime, thus the error you're getting. In your case your code would be:

// I'm assuming you have this declared in onCreate()
TextView myTextView = (TextView)findViewById(R.id.mytextview); 

if (i < 1) {
    myTextView.setText(getString(R.string.antwort_ja));
} else {
    myTextView.setText(getString(R.string.antwort_nein));
}

This also makes the assumption that you're calling this from your Activity. If not, you may need to call it from an instance of Context eg context.getString(R.string.antwort_ja);

strings.xml File:

<string name="firststring">Hello World</string>
<string name="secondtring">Welcome</string>

Activity or Fragment:

textView.setText(i < 1 ? getResources().getString(R.string.firststring) : getResources().getString(R.string.secondtring));

First declare your TextView Variable

private TextView mTextView;

in onCreate

mTextView  = (TextView) findViewById(R.id.mytextview);

after that you can work with it

if(i < 1) 
    System.out.println("Ja");
 else
    System.out.println("Nein");

Can be done with your TextView as below

if(i < 1) 
   mTextView.setText(getString(R.String.myJaText);
 else
    mTextView.setText(getString(R.String.myNeinText);

In Strings.xml you can write the text you are going to use in your textfield.

You can find Strings.xml into Res - Values :

<string name="myJaText">Ja</string>
<string name="myNeinText">Nein</string>

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