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 ~