简体   繁体   English

Android 应用程序:如何用 java 代码打印出一个字符串?

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

I am very new to Android programming!我对Android编程很陌生!

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:我如何实现这一点的第一个想法是在 xml 中有一个带有 String 资源的 TextView:

<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:然后通过 Java 代码访问该资源:

R.string.Entscheidung = "Ja";

but it doesnt work: Error:(13, 17) error: cannot assign a value to final variable Entscheidung但它不起作用:错误:(13, 17)错误:无法为最终变量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.对于常量字符串值(在您的情况下为“Ja”和“Nein”),您可以在名为strings.xml的文件中定义这些值,该文件位于res/values文件夹中。 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.对于 XML 布局文件,是的,您可以使用@string/前缀来引用这些字符串资源。 However, in your Java code you can only refer to these values using the getString() method using the R.string prefix.但是,在您的 Java 代码中,您只能使用R.string前缀的getString()方法来引用这些值。 Because these R.string values are final , they cannot be changed at runtime, thus the error you're getting.因为这些R.string值是final ,它们不能在运行时更改,因此会出现错误。 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);如果没有,您可能需要从Context实例调用它,例如context.getString(R.string.antwort_ja);

strings.xml File: strings.xml文件:

<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首先声明你的 TextView 变量

private TextView mTextView;

in onCreate在 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可以使用您的 TextView 完成,如下所示

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.在 Strings.xml 中,您可以编写要在文本字段中使用的文本。

You can find Strings.xml into Res - Values :您可以在Res - Values找到 Strings.xml :

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

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

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