Basic Android tutorial (1)-Use of textview label to change and display text labels

Source: Internet
Author: User

In the basic Android tutorial (5), we wroteHelloandroid
After that, I always felt that I had not written half a row.CodeSorry, so in this section, we willHelloandroid
AndTextview
The first contact of the text label. In this exampleLayout
CreateTextview
Object, and learn to defineRes/values/string. xml
The String constant in, and finally passTextview
OfSettext
Method, in the pre-loadProgramAt the beginning, changeTextview
Text.

 

First, let's take a look at the running result as follows:

 

 

First of all, where does the word "Welcome to Wei zhulin's blog" come from? We are in res-> values-> string. the XML contains the following ():

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Resources>
<String name = "hello"> Hello world, helloandroid! </String>
<String name = "app_name"> helloandroid </string>
<String name = "textview_text"> welcome to Wei zhulin's blog </string>

</Resources>

 

Loading "Welcome to Wei Zhilin's blog" isMain. xml
(Defined in the mobile phone layout Interface), as shown in the following code.@ String/Hello
Changed@ String/textview_text
.

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/textview_text"

/>
</Linearlayout>

 

In this way, we run helloandroid. in Java, the welcome page "Welcome to Wei zhulin's blog" will be displayed on the mobile phone screen. It seems that we have not written any code, but we are only in. I added one or two lines of XML to solve the problem. I feel a little uncomfortable to those who are used to programming. in fact, in helloandroid. java code writing can achieve the same effect.

 

Here, we first set main. XML returns to the original format and adds a line as it is. See the following figure (). Here, ID is used to locate the textview object in the Java class and control it:

 

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Textview

Android: Id = "@ + ID/mytextview"

Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
</Linearlayout>

 

The code in the main program helloandroid. Java is as follows:

 

Package com. Android. test;
Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. widget. textview;

public class helloandroid extends activity {
private textview mytextview;
Public void oncreate (bundle savedinstancestate) {
super. oncreate (savedinstancestate);
// load main. XML layout. mytextview: text is Hello
setcontentview (R. layout. main);
// use the findviewbyid function to find the textview object by ID
mytextview = (textview) findviewbyid (R. id. mytextview);
string welcome_mes = "Welcome to Wei zhulin's blog";

// Use the settext method to change the textview text to welcom_mes
Mytextview. settext (welcome_mes );

}
}

 

Both methods can achieve the same effect, but I suggest you use the first method to make it more standard. This section is here !! In the next section, we will talk about the five la s of Android. I hope you will continue to pay attention to them ~

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.