Basic controls commonly used on Android

TextView

This is a text control that displays text information.

Properties:

1. Android: ID (Set the id of the control. When you want to use the control in your Activity, you must use the ID to find the control.)

2. Android: Text (Text information, you need to display the text content of the control)

3. Android :textSize(to set the font size, the official unit is SP, but I suggest you use DP, because sp will change the font size as the system changes, dp will not, can be used according to the specific situation)

4. Android :textColor(to set the font color, it is recommended to write the color value under values in color.xml for reference)

Android :textStyle(Set font style, Blod (bold), italic(italic), normal(normal))

6. Android :gravity(uses the secondary property to control the position of text display when textView is specified as width and height or match-parent)

7. Android :drawableLeft(usually an image or drawable file, make it to the left of the text)

8. Android: Drawable top (usually an image or drawable resource file, make it display above the text)

9. Android :drawableBottom(usually an image or a drawable resource file, so that it appears below the text)

10. Android :drawableRight(usually an image or a drawable resource file)

11. The android: drawablePadding (the spacing between the text and images)

The code in the XML file is as follows:

<TextView // control ID Android :id =" @+ ID/XXX "//@+id/ XXX indicates that the new control is named XXX // Width and height Android :layout_width="wrap_content" Android :layout_height="wrap_content" //wrap_content or match_parent // text text Android :text="@string/hello_world"// Android :textSize="24sp" // sp as unit // font color Android :textColor="#0000FF" //RGB color Android :textStyle="normal"// Normal,bold,italic, Android :gravity="center"// Specifies the text alignment. Possible values include top, bottom, left, right, and center. // Specifies whether to display all contents in one line Android :singleLine="true" //true or false, the default is falseCopy the code

EditText

Another important feature of the EditText application is its ability to interact with the user. It allows the user to enter and edit content in the control. Similarly, the EditText application can configure properties similar to those of TextView.

1. Android :hint(This attribute specifies a hint of text that will disappear when the user enters anything)

2. Android :maxLine(Specify EditText to have a maximum of two lines, so that when you type more than two lines, the text will scroll up and the EditText will not continue to stretch)

3. Android :inputType(Limit of input text (numbers, letters, passwords))

The code in the XML file is as follows:

Android :layout_width="wrap_content" android:layout_width="wrap_content" android:layout_width="wrap_content" Android :layout_height="wrap_content" //wrap_content or match_parent // text text Android :text="@string/hello_world"// Android :hint="hello_world" android:hint="hello_world" android:hint="hello_world" Android :textSize=" 24SP "// sp as a unit // font color Android :textColor="#0000FF" //RGB color // font format Android :textStyle="normal"//normal,bold,italic, Android :gravity="center"// Specifies the text alignment. Possible values include top, bottom, left, right, and center. // Specifies whether to display all contents in one line Android :singleLine="true" //true or false, The default value is false. // Set the input type to Password. Android: Password ="true" // The input type will change to... // Set the input type to phoneNumber. Android :cursorVisible = "false" // True or false, the default is true to displayCopy the code

RadioGroup and RadioButton

RadioGroup is a radio combo box, which is used to box the RadioButton. In the absence of RadioGroup, the RadioButton can be all selected. In the case that multiple Radiobuttons are included by a RadioGroup, the RadioButton can only select one, thus realizing the effect of radio selection.

The code in the XML file is as follows:

<RadioGroup android:id="@+id/radio_group" android:layout_width="wrap_content" android:layout_height="wrap_content" // Set the arrangement of the RadioButton, Horizontal :orientation="horizontal" > <RadioButton android:id="@+id/rd1" Android :layout_width="wrap_content" Android :layout_height="wrap_content" // Set the text prompt following the radio selection. Android :text=" Beijing "// Set the text size Android :textSize="30sp" // Set the color of the text android:textColor="#0000FF" // font format Android :textStyle="normal"//normal,bold,italic, <RadioButton android:id="@+id/rd2" Android :layout_width="wrap_content" Android :layout_height="wrap_content" Android :textSize="30sp" Android :text=" Shanghai "/> </RadioGroup>Copy the code

Radio buttons to declare in RadioGroup, is to give him the RadioGroup add: setOnCheckedChangeListener (RadioGroup. OnCheckedChangeListener) listener.

CheckBox

CheckBox Check button,

IsChecked () : Checks whether it is selected

Listening state changes, you need to add: setOnCheckedChangeListener (CompoundButton. OnCheckedChangeListener);

The code in the XML file is as follows:

<CheckBox android:id="@+id/cb1" android:layout_width="match_parent" android:layout_height="wrap_content" Android :textSize="30sp" // Set the color of the text android:textColor="#0000FF" // font format Android :textStyle="normal"//normal,bold,italic, Default normal/> <CheckBox Android :id="@+id/cb2" Android :layout_width="match_parent" Android :layout_height="wrap_content" Android :textSize="30sp" Android :textColor="#0000FF"/>Copy the code

Button

This is a button control, to provide a basic button style, you can change the style according to the properties.

Properties:

1. Android: ID (Set the id of the control. When you want to use the control in your Activity, you must use the ID to find the control.)

2. Android: Background (button background color)

3. Android: Text (control display text content)

ImageView

This is a control to display images, images can be a network image, can be a resource file image, the so-called resource file image is you copy the image to the project’s drawable or mipmap to reference.

Properties:

1. Android: ID (Set the id of the control. When you want to use the control in your Activity, you must use the ID to find the control.)

2. App :srcCompat(Apply resource files to display images)

3. Android :scaleType(this is to set the image display type)

These are the main attributes, as well as the width and height Settings.

Summary of the basic application of Activity

Activity is intuitively understood as an interface on the mobile phone screen. The main function of Activity is to present the interface. Activity is one of the four components in the Android system, which can be used to display the View visual control. An Activity is a system module that interacts with users. Almost all activities interact with users. The specific role of interaction: one is display; Second, man-machine interaction.

Create your own Activity

Step 1: Define the display layout in the corresponding layout file;

Step 2: Define the Activity class by inheriting the Activity and overriding the onCreate() method

  • Find the corresponding XML layout file: setContentView(r.layout.main)
  • BTN =(Button)this.findViewById(R.i.button01);

Step 3: Register in Androidmanifest.xml.