简体   繁体   中英

Android beginner needs some knowledge

Noob Android developer here looking for clarification about this:

To show the message on the screen, create a TextView widget and set the text using setText() . Then add the TextView as the root view of the Activity 's layout by passing it to setContentView() .

Specifically what does it mean to set the text and root view mean? Thanks.

set the text

means set the text that displays on the TextView

root view

means the very base View of the Activity 's layout, the Activity is build by View s, so that the Activity is visiable, the root means the first tag in the layout xml file.

setText("Your Text")

This function sets the text which is given as arguement to the TextView widget (child).Its is almost like System.out.println(" ") or System.out.print(" ") which displays the string to screen.But here setText(" ") displays to mobile screen.Actually the child( TextView ) needs parent(Layout).So addView()` must be used to add this child to the parent.

setContentView()

Set the activity content from a layout resource. The resource will be inflated, adding all top-level views to the activity.To know more about this click this link ..

If you a complete newby, then probably "adding the TextView as the root view of the activity's layout" (whatever it means) is not the right place to start. Try to understand the very basics by learning your first project structure. Assuming that you work in Eclipse and haven't changed the default activity (MainActivity.java) and layout file (activity_main.xml) names when creating the project, do as follows:

  • Go to res/layout/activity_main.xml and add the following line to the TextView definition:

    android:id="@+id/tv"

So, your activity_main.xml should look like

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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" >

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

</RelativeLayout> 
  • Go to src/your.package.name.MainActivity.java and add the lines:

    TextView tv = (TextView) findViewById(R.id.tv);

    tv.setText("Your text");

If TextView is highlighted with red, press Ctrl+Shift+O (Windows) to import the needed classes. You should see:

package your.package.name;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView tv = (TextView) findViewById(R.id.tv);
    tv.setText("Your text");
}

}
  • Run your app.

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