Android tutorial (iv) -- Use of TextView

Source: Internet
Author: User

In this chapter, we will talk about the use of common controls.

The same applies to most chapters in the future. We will discuss the usage of some controls in detail. As long as these controls are combined, they are an application.

Let's take a look at how to use this control.

As you may find, the content of this control is defined in strings. xml in the values folder.

Then we only need to add a piece of code to it:


[Java]
<String name = "test"> Welcome to Joven \'s blog </string>

<String name = "test"> Welcome to Joven \'s blog </string>
Then bind to the control

 

[Java]
<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/test"/>

<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/test"/>


You can. Let's take a look at the effect.

 
 

 

In fact, there is another second method, which is also the most commonly used method and is obtained dynamically.

We add an ID to TextView and write it in main. xml as follows:


[Java]
<TextView
Android: id = "@ + id/tv1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/test"/>

<TextView
Android: id = "@ + id/tv1"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/test"/>
Then dynamically bind it to the activity and write it in TestDemo. java as follows:


[Java]
Package com. example. testdemo;
 
Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Import android. widget. TextView;
 
Public class TestDemo extends Activity {
 
Private TextView TV;
 
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Bind the textview control with the id of tv1 to the view.
TV = (TextView) findViewById (R. id. tv1 );
// Dynamically set the control content.
TV. setText ("Welcome! ");
}
 
}

Package com. example. testdemo;

Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Import android. widget. TextView;

Public class TestDemo extends Activity {

Private TextView TV;

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
// Bind the textview control with the id of tv1 to the view.
TV = (TextView) findViewById (R. id. tv1 );
// Dynamically set the control content.
TV. setText ("Welcome! ");
}

}

Now let's look at the effect:

 
 


In terms of priority, the program loads the resource file and then the activity. Therefore, the latter's settings will overwrite the former.

 

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.