Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...