简体   繁体   中英

Basic issue with Java/Android

I started learning java a few days ago, so I'm quite lost with it. I want to show a text received from an intent and make it look like this: "You've written : ." It isn't working at all and I'm only able to show the text from the intent.

Here's my code:

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

Besides, I'm trying to display the text written above in the first line of the page (or as many lines it takes) and, below, a label to insert a text together with a Button. The problem is that I can only see the text from the intent. Here's my code:

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

Thankyou very much, I wish I'll be answered soon.

Instead of System.out.println.

Do like this.

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

and Get the view in activity.

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

}

If you are trying to reference the TextView inside your layout it needs to referenced in the Activity. The above solution shows you how to handle that. setContentView(R.layout.file_name) is what should be used to reference a layout created inside an xml file stored in res/layout/file_name.xml or created manually inside your code. If you are going to call setContentView for a layout contructed inside your (Activity) it will take some more code. If the message is all is need to be displayed you can always call Toast.maketText(Context, message, Toast.LENGTH_SHORT).show(); for example. The other suggestion is Java developers are used to using System.out.println() to debug to the console. In Android they have a neat feature we use called LogCat that can display messages going on inside your code by saying (example for debugging purposes) Log.d(TAG, message); Where TAG is usually a constant defined for the Activity,Fragment, etc. you are in so you can see where the message is coming from and display whatever value you normally would have used in System.out.println().

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM