Java/Android的基本问题

Java/Android的基本问题,java,textview,Java,Textview,我几天前开始学习java,所以我对它很迷茫。 我想显示从intent收到的文本,并使其看起来像这样: “你写了:” 它根本不起作用,我只能从意图中显示文本 这是我的密码: // Create the text view TextView textView = new TextView(this); textView.setTextSize(40); System.out.print("You've written: "); textView.setText(message); System.ou

我几天前开始学习java,所以我对它很迷茫。 我想显示从intent收到的文本,并使其看起来像这样: “你写了:” 它根本不起作用,我只能从意图中显示文本

这是我的密码:

// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
System.out.print("You've written: ");
textView.setText(message);
System.out.print(".");

// Set the text view as the activity layout
setContentView(textView);
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".DisplayMessageActivity" >

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

    <EditText android:id="@+id/edit_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage"/>

</RelativeLayout>
此外,我试图在页面的第一行显示上面写的文本(或尽可能多的行),并在下面显示一个标签,用按钮插入文本。问题是我只能从意图中看到文本。 这是我的密码:

// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
System.out.print("You've written: ");
textView.setText(message);
System.out.print(".");

// Set the text view as the activity layout
setContentView(textView);
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".DisplayMessageActivity" >

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

    <EditText android:id="@+id/edit_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />

    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage"/>

</RelativeLayout>


非常感谢,我希望能尽快得到答复。

而不是System.out.println

像这样做

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

如果您试图在布局中引用TextView,则需要在活动中引用它。上面的解决方案向您展示了如何处理这个问题。setContentView(R.layout.file_name)应该用来引用在res/layout/file_name.xml中存储的xml文件中创建的布局,或者在代码中手动创建的布局。如果要为在(活动)中构造的布局调用setContentView,则需要更多的代码。如果需要显示的消息是全部,则始终可以调用Toast.maketText(Context,message,Toast.LENGTH_SHORT).show();例如另一个建议是Java开发人员习惯于使用System.out.println()对控制台进行调试。在安卓系统中,它们有一个整洁的功能,我们称之为LogCat,它可以显示代码中的消息,比如说(用于调试的示例)
Log.d(标签、消息);其中标记通常是为活动、片段等定义的常量。您所在的位置可以查看消息的来源,并显示System.out.println()中通常使用的值。

您使用的是RelativeLayout,而不是placement构造。它们将被写在视图左上角的每个顶部。@user2211939请这样尝试。它可能会帮助您。@user2211939您遇到的错误。你写了什么。它不起作用也许我把它放错地方了,但它给了我一个错误:layout_xml无法解析为variabl@user2211939我刚刚给出了示例
setContentView(layout\uxml)。在您的案例中输入布局名称。