Article Directory
Introduction to Button
Button inherits from TextView and is mainly used to generate a button on the UI interface. When the user clicks the button, an OnClick event is triggered. Specify the background color or background image for the button through the android: backgroud attribute.
The Button class implements the operation of the button component by inheriting the methods of the parent class. The following table lists the common methods of the Button class.
Methods of the Button class
method | Function description |
---|---|
onKeyDown() | When the user presses a key, the method is called |
onKeyUp() | When the user key bounces up, this method is called |
onKeyLongPress() | When the user keeps the key pressed, the method is called |
onKeyMultiple () | This method is called when the user presses the key multiple times |
invalidateDrawable() | Used to refresh Drawable objects |
onPreDraw() | Used to set the view display, such as adjusting the boundary of the scroll axis before the view is displayed |
setOnKeyLicenses () | Used to set the button monitor |
setOnClickListcner() | Used to set the click listener |
Code demo
Let's take set(nClickListener() as an example to demonstrate the use of Button, TextView and EditView through a simulated login operation. The
interface layout code is as follows
<?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:layout_gravity="center_horizontal"
android:orientation="vertical">
<!-- 标题 1 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="用户登录"
android:textSize="35sp" />
<!-- 用户名 2 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:" />
<EditText
android:id="@+id/userNameTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- 密码 3 -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码: " />
<EditText
android:id="@+id/passwordTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"/>
</LinearLayout>
<!-- 登录按钮 4 -->
<Button
android:id="@+id/loginBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录" />
<!-- 成功或失败提示 5 -->
<TextView
android:id="@+id/tipsTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="显示成功或失败"
android:visibility="gone" />
</LinearLayout>
The code is explained as follows: label 1 shows the title of the current interface, and the font size of TextView is set to 35sp, which is relatively eye-catching; label 2 combines TextView and EditText through LinearLayout to receive the input of the user name; label 3 Combine TextView and EditText through LinearLayout to receive password input. The inputType of EditText is set to textPassword, indicating that the text box is used as a password box; label 4 defines a login button, which is used to achieve login in Activity Business logic processing; label 5 defines a TextView, which is used to display the prompt after the user login succeeds or fails, such as the user name does not exist, the password is wrong, etc. By setting android: visibility = "gone", this prompt is hidden by default.
Next, implement the login business logic in the corresponding Activity
code show as below
package com.qst.demo2;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class LoginActivity extends AppCompatActivity {
EditText userNameTxt; //用户名
EditText passwordTxt; //密码
Button logingBtn; //登录按钮
TextView tipsTv; //提示
@Override //重写父类的onCreate方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //调用父类的onCreate方法创建Activity
setContentView(R.layout.login); //设置布局为login
//初始化各个组件
userNameTxt = (EditText) findViewById(R.id.userNameTxt);
passwordTxt = (EditText) findViewById(R.id.passwordTxt);
tipsTv = (TextView) findViewById(R.id.tipsTxt);
logingBtn = (Button) findViewById(R.id.loginBtn);
//实现单击Button的监听器,并采用匿名类的形式
logingBtn.setOnClickListener(new View.OnClickListener() {
@Override //重写接口的方法
public void onClick(View v) {
String userName = userNameTxt.getText().toString(); //获取用户名
String passWord = passwordTxt.getText().toString(); //获取密码
//判断用户名
if (!"admin".equals(userName)) {
tipsTv.setText("用户名不存在!");
tipsTv.setVisibility(View.VISIBLE);
return;
}
if (!"1".equals(passWord)) {
tipsTv.setText("密码不正确");
tipsTv.setVisibility(View.VISIBLE);
return;
}
if ("admin".equals(userName) && "1".equals(passWord)) {
tipsTv.setText("登录成功!");
tipsTv.setVisibility(View.VISIBLE);
}
}
});
}
}
The code is explained as follows: defines an attribute variable userNameTxt of EditText type, which is used to obtain the object passed by the interface. The definition function of other attributes is similar, and then each attribute variable is initialized, and the attribute variable is assigned to enable it to follow up Business logic operations. The business logic of the login button loginBtn is realized. The logic is relatively simple. If the user name is invalid, it will display "Username does not exist"; if the password is incorrect, it will display "Password is incorrect!"; if the user enters both the user name and password If it is correct, it will display "Login successful!".
Declare Activity running results in the AndroidMainfest.xml manifest file