Common controls
Last time, we talked about layout: linear layout, table layout, frame layout and relative layout. This layout is the way to design a framework of content on the whole mobile terminal, and how to design the interface. Finally, in the interface, controls are placed. The so-called controls are components that programmers can control and that users can interact with programs in the future.
1. Text control
TextView: mainly used to display content to users. Equivalent to a label, only display
EditText: can be used to edit content. It is equivalent to a text box and can be edited
Control is the most important thing to understand its properties. After understanding the properties, it will be easier.
Omit: anroid: leading
TextView properties Java code
Textset the content of TextView settext (text)
textColor sets the color of the text SetTextColor
textSize sets the size of the text; Settextsize (size)
gravity defines how TextView is displayed in the x-axis and y-axis directions
hint: prompt information setHint(int);
EditText property: equivalent to a text box
Lines: sets the number of lines setLines(int)
singleLine single line setSingleLine(boolean)
Password sets whether the password is settransformationmethod (parameter)
phoneNumber setting content can only be phone number setInputType(InputType)
event listeners
setOnKeyListener listens for keyboard events
When designing the layout in front, write code in the following file;
active_main.xml
<!-- Enter the text box, and display View Synchronous display; EditText--><![CDATA[TextView On; The most important thing is to understand the principle; Homework: email, mobile phone password, etc; The problem just now is: the height is directly extended to the whole mobile phone screen; --> ]]> <EditText android:id="@+id/uname" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="enter one user name" /> <TextView android:layout_width="match_parent" android:layout_height="50dp" android:id="@+id/unameInfo" android:background="@color/purple" android:text="test" android:textSize="20dp"/>
public class MainActivity extends AppCompatActivity { //First define two variables; If it is red, press and hold Alt+Enter to import related classes; EditText uname; TextView unameInfo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //-------------------------------------------- //How to write background code; Operate the previous control through background code //Do you need to get this control??? uname=(EditText) findViewById(R.id.uname); unameInfo=(TextView) findViewById(R.id.unameInfo); //In the future, the way of control is like this; //Next, this is a listening method. You need to add a listener //The problem of red wavy lines is to use anonymous classes to import their methods; Still Alt+Enter to implement the abstract method uname.setOnKeyListener(new View.OnKeyListener(){ @Override public boolean onKey(View view, int i, KeyEvent keyEvent) { //Accept this input; String str=uname.getText().toString(); System.out.println(str+"-----"); unameInfo.setText(str); return false; } }); } }
2. Button control
Button controls are the button buttons and ImageButton controls we usually see. They are often used to manipulate buttons to realize corresponding commands, such as many search, login, registration, various click games on mobile phones, etc.
Button control name: what is it?
Button
ImageButton:
The difference between this and Button is that it has no text, only what? Image, so you need to make its image path.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- Button component. You can define 2 buttons without too many.--> <Button android:layout_width="100dp" android:layout_height="wrap_content" android:id="@+id/btnLogin" android:text="Sign in"/> <ImageButton android:layout_width="100dp" android:layout_height="wrap_content" android:src="@mipmap/hjamera" android:id="@+id/btn222"/> <!-- Add a control for information prompt--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/msg" android:textSize="20dp"/> </LinearLayout>
Background code
package com.aaa.zyg001; // This is the definition of the package //Import import related classes; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.TextView; import org.w3c.dom.Text; public class MainActivity extends AppCompatActivity { //1. Declare variables to represent 2 button objects; Button btn1; ImageButton btn2; //Information prompt variables; TextView msg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //------------------------------------------------------------------ btn1=(Button) findViewById(R.id.btnLogin); btn2=(ImageButton)findViewById(R.id.btn222); //Similarly, you need to add the object of prompt information; msg=(TextView)findViewById(R.id.msg); //Let's observe. Is it the same as the text box and password box just demonstrated???? //Next, set the events of the two buttons to test btn1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View view) { //Setting the content prompt to the information prompt variable is the same as what we just said msg.setText("Look at my reaction!!!"); } }); } }
Homework: test or expand it.