如何在Android应用程序中打印消息

时间:2022-04-26 22:24:53

Hi I'm starting to learn how to write Android Apps for the first time but I'm running into problems.

你好,我第一次开始学习如何编写Android应用程序,但是我遇到了一些问题。

When I go to the string.xml file to enter a message to be printed on the screen. Instead of printing my message it prints "Hello World." It should also be noted that I'm following this tutorial:

当我进入弦。输入要在屏幕上打印的消息的xml文件。它不是打印我的信息,而是打印“Hello World”。还应该注意的是,我正在遵循本教程:

http://www.raywenderlich.com/78574/android-tutorial-for-beginners-part-1#comments

http://www.raywenderlich.com/78574/android-tutorial-for-beginners-part-1的评论

And in their tutorial there is a statement that says to change a hello world string to my own custom message. Problem is when I opened the file there was no hello world string. (But the app prints hello world anyway.) And when I go out of my way to make a hello world statement per the example.

在他们的教程中,有一个语句说要将hello world字符串更改为我自己的自定义消息。问题是当我打开文件时,没有hello world字符串。(但这款应用还是会打印hello world。)当我用我的方法做一个hello world语句的时候。

Before I modified the file:

在我修改文件之前:

  <resources>
   <string name="app_name">OMG Android</string>
   <string name="action_settings">Settings</string>
 </resources>

After I modified the file:

在我修改了文件之后:

 <resources>
   <string name="app_name">OMG Android</string>
   <string name="action_settings">Settings</string>
   <string name="Hello_World">Dpolaristar is programming in Android</string>
 </resources>

The app prints Hello World without the personal message anyway. I'm confused, can anyone tell me if there is some sort of automatic override or somewhere else in the project where it says give the ability to print Hello World that I can override. I already looked but I can't find anything.

不管怎样,这款应用打印Hello World的时候没有任何个人信息。我很困惑,谁能告诉我在这个项目中是否有某种自动覆盖或者在其他地方它说给我打印Hello World的能力我可以覆盖。我已经找过了,但什么也没找到。

My problem also seems to be oddly specific which makes me think I either touched something I shouldn't have or maybe I'm using a different version of Android Studios then the author of the tutorial.

我的问题似乎也非常具体,这让我觉得我要么碰了不该碰的东西,要么我用的是另一个版本的Android studio,然后是教程的作者。

Help?

帮助吗?

EDIT:

编辑:

I managed to get to the text view of the activity_main.xml to add the reference. Problem is there is no textview section to add under.

我设法找到了activity_main的文本视图。添加引用的xml。问题是没有textview部分可以添加到下面。

Here is a copy and paste of the file which I DID NOT change for reference:

这是我没有更改的文件的拷贝和粘贴,以供参考:

     <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"
    android:layout_height="match_parent" android:fitsSystemWindows="true"
    tools:context=".MainActivity">

   <android.support.design.widget.AppBarLayout   android:layout_height="wrap_content"
    android:layout_width="match_parent"

android:theme="@style/AppTheme.AppBarOverlay">

android:主题= " @style / AppTheme.AppBarOverlay " >

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"  app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

    <android.support.design.widget.FloatingActionButton  android:id="@+id/fab"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />

    </android.support.design.widget.CoordinatorLayout>       

EDIT 2:I've looked around my project and have found that in the DESIGN view of the content_main.xml I does show the value for the string I typed just not when I run the app ON MY TABLET!

编辑2:我查看了我的项目,发现在content_main的设计视图中。当我在我的平板电脑上运行应用程序时,我确实显示了我输入的字符串的值!

Does this give anyone any clues?

这能给任何人提供线索吗?

4 个解决方案

#1


1  

first of all the property name must be "hello_world", don´t use capital characters, it will cause problems in your proyect!

首先必须“hello_world”属性名,不要´用大写字符,它会导致你的问题proyect !

<string name="hello_world">Darryl is learning Android!</string>

Then just add the reference:

然后添加参考:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>

Then you will see the sentence Darryl is learning Android! displayed in your TextView.

然后你会看到这个句子Darryl正在学习Android!显示在你的TextView中。

The value <string name="hello_world">Darryl is learning Android!</string> must be setted inside /res/values/strings.xml

这个值 Darryl正在学习Android! 必须设置在/res/values/string .xml中

To open the layout that contains the TextView go to /res/layout/activity_main.xml or /res/layout/activity_my.xml like in your example.

打开包含TextView的布局到/res/layout/activity_main。xml或/ res / layout / activity_my。就像您的示例中的xml一样。

#2


3  

Based on the tutorial you were following it appears as if you are generating just a blank layout resource file which by default populates with the static value of "Hello World". In order to manipulate this value with the String resource you created you will need to adjust the Layout file where the Textview is being stored.

根据您正在学习的教程,它显示为您仅仅生成一个空白的布局资源文件,默认情况下,该文件使用“Hello World”的静态值填充。为了使用创建的字符串资源操作此值,需要调整存储Textview的布局文件。

如何在Android应用程序中打印消息

Now based on your code you would need to edit the android:text section of your Layout file. I generated a string very similar to yours however I edited the resource file above to set the text properly. Now you can see the value updated with the custom string I implemented.

现在,根据您的代码,您需要编辑布局文件的android:text部分。我生成了一个非常类似于您的字符串,但是我编辑了上面的资源文件以正确地设置文本。现在您可以看到用我实现的自定义字符串更新的值。

如何在Android应用程序中打印消息

如何在Android应用程序中打印消息

Any time you are referencing a String resource from your Strings.xml file you will use the syntax: android:text="@string/STRINGNAME". This tells the IDE you are pulling the string from your Strings.xml resource file and that you don't want the exact text you are inputting.

当您从字符串引用字符串资源时。您将使用语法:android:text=“@string/STRINGNAME”的xml文件。这告诉IDE你正在从你的字符串中拉出字符串。xml资源文件,并且您不希望输入确切的文本。

EDIT

编辑

Ok with the addition of your Layout XML you clarified a bunch :) I can see you generated a Blank activity which is configured using a Coordinator Layout. A secondary layout should have been auto-generated with that labeled content_main.xml. This is where you need to look for your TextView. You are looking at the main layout but not the container found within the unit.

好的,随着您的布局XML的添加,您澄清了一束:)我可以看到您生成了一个空白活动,它是使用一个协调器布局来配置的。二级布局应该使用标有content_main.xml的标签自动生成。这是您需要查找TextView的地方。您正在查看主布局,而不是单元中找到的容器。

如何在Android应用程序中打印消息

#3


2  

Although, the tutorial in the link has plenty of info, it is not well organized/structured.

尽管链接中的教程包含了大量的信息,但是它并没有很好的组织和结构。

In order to show a "Hello World!" text, you should paste this in the xml of your activity (MainActivity - maybe) :

为了显示“Hello World!”文本,您应该将其粘贴到活动的xml (MainActivity - maybe)中:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>

If you really want to use "Hello World!" as a string resource just add in strings.xml

如果您真的想使用“Hello World!”作为字符串资源,请添加string .xml

<string name="text_4_my_hello_world_example">Hello World!</string>

And change in the code above the following

并更改上面的代码如下

android:text="@string/text_4_my_hello_world_example"

Good luck and have fun learning!

祝你好运,学习愉快!

#4


1  

Because in your layout file, TextView's text attribute must be like this:

因为在布局文件中,TextView的文本属性必须是这样的:

android:text="@string/hello_world"

If you create a new string in strings.xml:

如果您在strings.xml中创建了一个新字符串。

<string name="my_string">My Beautiful String</string>

And call it in your TextView:

在TextView中调用它:

<TextView
 ...
 android:text="@string/my_string"
 ... />

This should work.

这应该工作。

#1


1  

first of all the property name must be "hello_world", don´t use capital characters, it will cause problems in your proyect!

首先必须“hello_world”属性名,不要´用大写字符,它会导致你的问题proyect !

<string name="hello_world">Darryl is learning Android!</string>

Then just add the reference:

然后添加参考:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"/>

Then you will see the sentence Darryl is learning Android! displayed in your TextView.

然后你会看到这个句子Darryl正在学习Android!显示在你的TextView中。

The value <string name="hello_world">Darryl is learning Android!</string> must be setted inside /res/values/strings.xml

这个值 Darryl正在学习Android! 必须设置在/res/values/string .xml中

To open the layout that contains the TextView go to /res/layout/activity_main.xml or /res/layout/activity_my.xml like in your example.

打开包含TextView的布局到/res/layout/activity_main。xml或/ res / layout / activity_my。就像您的示例中的xml一样。

#2


3  

Based on the tutorial you were following it appears as if you are generating just a blank layout resource file which by default populates with the static value of "Hello World". In order to manipulate this value with the String resource you created you will need to adjust the Layout file where the Textview is being stored.

根据您正在学习的教程,它显示为您仅仅生成一个空白的布局资源文件,默认情况下,该文件使用“Hello World”的静态值填充。为了使用创建的字符串资源操作此值,需要调整存储Textview的布局文件。

如何在Android应用程序中打印消息

Now based on your code you would need to edit the android:text section of your Layout file. I generated a string very similar to yours however I edited the resource file above to set the text properly. Now you can see the value updated with the custom string I implemented.

现在,根据您的代码,您需要编辑布局文件的android:text部分。我生成了一个非常类似于您的字符串,但是我编辑了上面的资源文件以正确地设置文本。现在您可以看到用我实现的自定义字符串更新的值。

如何在Android应用程序中打印消息

如何在Android应用程序中打印消息

Any time you are referencing a String resource from your Strings.xml file you will use the syntax: android:text="@string/STRINGNAME". This tells the IDE you are pulling the string from your Strings.xml resource file and that you don't want the exact text you are inputting.

当您从字符串引用字符串资源时。您将使用语法:android:text=“@string/STRINGNAME”的xml文件。这告诉IDE你正在从你的字符串中拉出字符串。xml资源文件,并且您不希望输入确切的文本。

EDIT

编辑

Ok with the addition of your Layout XML you clarified a bunch :) I can see you generated a Blank activity which is configured using a Coordinator Layout. A secondary layout should have been auto-generated with that labeled content_main.xml. This is where you need to look for your TextView. You are looking at the main layout but not the container found within the unit.

好的,随着您的布局XML的添加,您澄清了一束:)我可以看到您生成了一个空白活动,它是使用一个协调器布局来配置的。二级布局应该使用标有content_main.xml的标签自动生成。这是您需要查找TextView的地方。您正在查看主布局,而不是单元中找到的容器。

如何在Android应用程序中打印消息

#3


2  

Although, the tutorial in the link has plenty of info, it is not well organized/structured.

尽管链接中的教程包含了大量的信息,但是它并没有很好的组织和结构。

In order to show a "Hello World!" text, you should paste this in the xml of your activity (MainActivity - maybe) :

为了显示“Hello World!”文本,您应该将其粘贴到活动的xml (MainActivity - maybe)中:

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"/>

If you really want to use "Hello World!" as a string resource just add in strings.xml

如果您真的想使用“Hello World!”作为字符串资源,请添加string .xml

<string name="text_4_my_hello_world_example">Hello World!</string>

And change in the code above the following

并更改上面的代码如下

android:text="@string/text_4_my_hello_world_example"

Good luck and have fun learning!

祝你好运,学习愉快!

#4


1  

Because in your layout file, TextView's text attribute must be like this:

因为在布局文件中,TextView的文本属性必须是这样的:

android:text="@string/hello_world"

If you create a new string in strings.xml:

如果您在strings.xml中创建了一个新字符串。

<string name="my_string">My Beautiful String</string>

And call it in your TextView:

在TextView中调用它:

<TextView
 ...
 android:text="@string/my_string"
 ... />

This should work.

这应该工作。