0

私は数日前にJavaを学び始めたので、それでかなり迷っています。インテントから受け取ったテキストを表示して、「You've written : .」のように表示したいと考えています。まったく機能しておらず、インテントからのテキストしか表示できません。

これが私のコードです:

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

どうもありがとう、私はすぐに答えられることを望みます。

4

2 に答える 2

1

System.out.println の代わりに。

このようにしてください。

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

ビューのアクティビティを取得します。

public void onCreate(Bundle bundle) {
    .........
   setContentView(layout_xml);
   TextView textView = (TextView)findViewById(R.id.topTextView);
   textView.setText("You've written: " + message + " .");
   .........

}
于 2013-03-26T14:36:22.720 に答える
0

レイアウト内で TextView を参照しようとしている場合は、Activity で参照する必要があります。上記のソリューションは、それを処理する方法を示しています。setContentView(R.layout.file_name) は、res/layout/file_name.xml に保存された xml ファイル内で作成されたレイアウト、またはコード内で手動で作成されたレイアウトを参照するために使用する必要があります。(Activity) 内で構築されたレイアウトに対して setContentView を呼び出す場合は、さらにコードが必要になります。メッセージをすべて表示する必要がある場合は、いつでも Toast.maketText(Context, message, Toast.LENGTH_SHORT).show(); を呼び出すことができます。例えば。もう 1 つの提案は、Java 開発者が System.out.println() を使用してコンソールにデバッグすることに慣れていることです。Android には、LogCat と呼ばれる優れた機能があり、(デバッグ目的の例) Log.d(TAG, メッセージ); TAG は通常、現在のアクティビティ、フラグメントなどに対して定義された定数です。そのため、メッセージの送信元を確認し、System.out.println() で通常使用する値を表示できます。

于 2013-03-27T12:46:39.680 に答える