如何显示的字符串中的Andr​​oid应用程序

如何显示的字符串中的Andr​​oid应用程序

本文介绍了如何显示的字符串中的Andr​​oid应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打乱了我的preVIOUS解释,好吧,我再试一次。

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

我有一个按钮和一个文本框,输入文本到文本字段,preSS的按钮,它的意思是带你到另一个页面,并显示输入的文本,buuut

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

好了,所以,我要带下划线的消息,向左(一个从它的瘦箭头指向)是越来越称为Android的变量:文本=@字符串/消息,但是,机器人:文本= @字符串/消息的,而不是从具有的strings.xml称之为<字符串名称=消息>也就是说< /串> 所以不管我进入到文本字段,第二页总会说,字

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"

我真的希望是有道理的Dx

I really hope that makes sense Dx

推荐答案

这是一个令人困惑的问题。但如果你想字符串变量为字符串从XML文件,你应该做到以下几点:

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.

既然你已经改变了你的问题,我想我知道你想这样做,我已经编辑我的答案是什么。正如约翰在他的文章中提到,你需要做的第一件事就是调用的setContentView 调用 super.onCreate 后直接。这意味着你将能够从你的XML布局访问TextView的和它的文本更改为已经传递到你的活动文本。我将在下面给出一个例子。

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.

首先,你需要一个ID添加到布局文件名为activity_display_message.xml文本视图:

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"/>

然后,如果我理解你想要做正确的的onCreate 方法是什么包含以下内容:

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

这篇关于如何显示的字符串中的Andr​​oid应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 23:17