Android studio APP development chapter 2 Button and Textview functions

Hits: 0

APP Development Chapter 2 Button and TextView

This chapter will introduce the most basic textview and button controls in Android app development


The common properties of the control textview are: width and height, color, margin, content, click event, etc. Mainly used to display some text in the interface. It can be displayed anywhere on the interface by setting, the font size and color, the background color of the [text box , etc. can be adjusted.]

Create several new textviews

  1. Open the first project FirstProject created last time, and select app->src->main->res->layout->activity_main.xml in the file bar on the left as shown in the

Two ways to create TextView

1: Direct handwriting
Add the following code in the code interface:

    android:text="Hello World two"

2: Drag the textview control to join
Open the design interface, find the textview control, drag it to join, the design is in the lower left corner of the following figure:
At this time, a part of TextView code will be automatically added to the Text code interface, and you can add some personalized content. Refer to the code above.

code explanation

layout_width layout_height


layout_width and layout_height represent the width and height of the TextView control block, respectively. wrap_content is adaptive. It will automatically adjust the size according to the size and length of the text. The advantage is that the font will not burst the control block, and the disadvantage is that it is not easy to design the layout.
In addition to wrap_content, there is match_parent, that is, the entire row is filled or the entire column is filled. When both width and height are set to match_parent, the entire interface will be filled.


In addition to that, there are custom sizes. as follows:


dp is the unit, applicable to length, width and height.

text textColor textSize

android:text="Hello World two"

Set the text content, text color, and text size of the TextView control respectively.
The text size unit is sp.
It is recommended to add colors to the color file in the res->values ​​directory, and then reference it in the layout layout file.
As shown in the figure:
Referenced in the layout file in the following format.


The string to be entered in text can also be defined in the strings of values, and then referenced in the layout layout file.
As shown in the figure:
In the layout layout file, it is referenced as follows:


Similarly, data such as dp sp can also be declared in the dimens of values. If there is no dimens file, you can right-click values ​​to create a Values ​​resource file.

singleLine maxLines maxHeight

singleLine is to display all text in one line. When the text to be displayed in a textview control is very long, the control may not be able to display all of it. In this case, set it to singleLine to automatically omit the redundant part and display it in one line. code show as below:


maxLines can be set to display in several lines, the code is as follows:


Can be arbitrarily set to 1, 2, 3 or other numbers.


Button is actually a special case of TextView, and the functions that can be realized by TextView can also be realized. Button is mainly used to set click events, TextView can also, and the method is the same.
The text settings of the Button are the same as those of the TextView, so I won’t repeat them. Here is the click event. First create a new Button, the method is the same as creating a new TextView.

set click event

After clicking, the interface jumps

The code interface after the new button is created is shown in the figure:
Step 1:
Add an id to the Button, that is, give the Button a unique name to distinguish it from other controls. The text in text is not an id.
code show as below:


Step 2:
Open MainActivity and add the following code to onCreate:

findViewById( View.OnClickListener(){
    public void onClick(View v) {
        Intent intent=new Intent(MainActivity.this,FirstActivity.class);

findViewById is to find the View control by id (Button is a special case of View control). Then set a click event onClick. Declare an Intent intent in onClick to convey the intent of clicking the Button to achieve interface jumping.

Click to pop up text

You may also like...

Leave a Reply

Your email address will not be published.