简体   繁体   中英

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

在此处输入图片说明

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

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);
String entered_message = getRessources().getString(R.string.entered_message);

However your String is empty. Change it to:

<string name="entered_message">Here some text</string>

Furthermore you don't add the TextView to your Layout. You have two choices:

(1): Add the View to the current Layout by for example:

LinearLayout layout = (LinearLayout) findViewById(R.id.content)
layout.addView(textView);

(In your main xml file the layout should have: android:id="@+id/content").

(2): Add the TextView directly to your XML file and assign the string inside of the xml (better approach):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:id="@+id/content">
  <TextView android:id="@+id/myTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/entered_message"
  />
</LinearLayout>

I'm confused on exactly what your problem is here but you have a couple problems if you want your TextView to display the message. This line here

TextView textView = new TextView(this);

will create a new TextView , not reference the one you have in your xml. And it won't show because you haven't added it to your contentView . So, when you call setContentView(R.layout.activity_display_message); what will be shown is what is in activity_display_message . If you want to access that View then you need to give it an id in your xml file and access it with findViewById() . So give it an id

<TextView
android:id="@+id/tv1"   // give it an id here
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/entered_message" />

then access it after calling setContentView(...)

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);

      Intent intent = getIntent();
      String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
      setContentView(R.layout.activity_display_message);
      TextView tv = (TextView) findViewById(R.id.tv1);  // reference it here with the id you gave it in the xml

}

Now that you have referenced the TextView , you can call setText() , setTextSize() , etc.. if need be.

I'm not exactly sure what String you want where so its hard to give much more help without a better explanation. But note that

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

will assign the message variable to whatever is passed in your Intent from the calling Activity .

First you have to create a layout, it looks like you already did:

layout.xml

<TextView
    android:id="@+id/root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

Now you create your string value:

string.xml

<string name="entered_message">Hello everyone!!</string>

Now you have to get the TextView in your activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout);
    //^^ do this first!!

    //Intent intent = getIntent();
    //String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    //^^ dont do this

    // do this:
    String message = this.getString(R.string.entered_message);
    TextView textView = (TextView)this.findViewById(R.id.root);
    textView.setTextSize(40);
    textView.setText(message);
}

and there you go! That all should show the text on the screen.

<string name="entered_message">Hello everyone!!</string>

Now you have to get the TextView in your activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout);
    //^^ do this first!!

    // do this:

    TextView textView = (TextView)this.findViewById(R.id.root);

    textView.setText(R.string.entered_message);
}

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