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

标签 java android xml android-studio

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

当我转到 string.xml 文件输入要在屏幕上打印的消息时。它没有打印我的消息,而是打印了“Hello World”。还应该注意的是,我正在学习本教程:

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

在他们的教程中有一条声明说要将 hello world 字符串更改为我自己的自定义消息。问题是当我打开文件时没有 hello world 字符串。 (但该应用程序无论如何都会打印 hello world。)当我不厌其烦地根据示例制作一个 hello world 语句时。

在我修改文件之前:

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

修改文件后:

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

无论如何,该应用程序都会打印 Hello World 而没有个人消息。我很困惑,任何人都可以告诉我是否有某种自动覆盖或项目中的其他地方它说能够打印我可以覆盖的 Hello World。我已经看过了,但找不到任何东西。

我的问题似乎也很奇怪,这让我觉得我要么触及了我不应该触及的东西,要么我使用的 Android Studio 版本与本教程的作者不同。

帮忙吗?

编辑:

我设法进入 activity_main.xml 的 TextView 以添加引用。问题是没有要添加的 TextView 部分。

这是我没有更改以供引用的文件的复制和粘贴:

     <?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.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>       

编辑 2:我查看了我的项目,发现在 content_main.xml 的设计 View 中,我确实显示了我键入的字符串的值,只是在我的平板电脑上运行应用程序时没有显示!

这会给任何人任何线索吗?

最佳答案

根据您所遵循的教程,您似乎只生成了一个空白布局资源文件,该文件默认填充静态值“Hello World”。为了使用您创建的 String 资源操作此值,您需要调整存储 Textview 的布局文件。

enter image description here

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

enter image description here

enter image description here

任何时候从 Strings.xml 文件引用字符串资源时,您都将使用语法:android:text="@string/STRINGNAME"。这告诉 IDE 您正在从 Strings.xml 资源文件中提取字符串,并且您不需要输入的确切文本。

编辑

好的,通过添加您的布局 XML,您澄清了很多 :) 我可以看到您生成了一个空白 Activity ,该 Activity 是使用协调器布局配置的。应该使用标记为 content_main.xml 的辅助布局自动生成。这是您需要查找 TextView 的地方。您看到的是主布局,而不是单元内的容器。

enter image description here

关于java - 如何在 Android 应用程序中打印消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34210742/

相关文章:

android - @VisibleForTesting 没有按预期工作

php - 如何使用 LOAD XML LOCAL INFILE 将数据从 xml 插入到 mysql

使用 JFileChooser 后 Java JSlider 不显示

java - 如何以非阻塞方式从 WebClient bodyToMono 或 toEntity 获取对象

Java JMenu - 选中和取消选中的功能

java - 如何在Android中将捕获的图像压缩到特定大小?

java - 将 GUI 添加到我的应用程序

android - list 中的使用功能加速度计

c++ - 为什么 boost::property_tree::read_xml 抛出读取有效的 Office Open XML?

c# - 在 XML 文档中使用 C# 从不同分支的父节点检索叶子