Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
525 views
in Technique[技术] by (71.8m points)

java - How do I display a string in an Android app?

I MUCKED UP MY PREVIOUS EXPLANATION, ok, I'll try again

I have a button, and a text field, you enter text into the text field, press the button, and it's meant to take you to another page and display the entered text, buuut

enter image description here

Ok, so, I want the underlined message to the left (the one with the skinny arrow pointing from it) to be the variable getting called in android:text="@string/message" but, android:text="@string/message" is instead calling it from the strings.xml which has <string name="message">words</string> so no matter what I enter into the text field, the second page will always say, "words"

I really hope that makes sense Dx

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is a confusing question. But if you want the string variable to be the string from your xml file you should do the following:

String message = this.getResources().getString(R.string.entered_message);

However the way you're doing things here seems very strange. But without knowing exactly what you're trying to do I can't be sure.

Since you've changed your question I think I know what you want to do so I've edited my answer. As John mentioned in his post the first thing you need to do is call setContentView directly after the call to super.onCreate. This means you will be able to access the TextView from your xml layout and change its text to the text which has been passed into your activity. I will give an example below.

Firstly you need to add an id to your text view in the layout file called activity_display_message.xml:

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

Then, if I've understood what you want to do correctly your onCreate method contain the following:

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);

Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

TextView textView = (TextView)findViewById(R.id.my_text_view);
textView.setTextSize(40);
textView.setText(message);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...