【Android】学习day05|简单登陆页面的实现|监听代码

实现效果如下图所示

 

 

 

 实现代码【部分】

MainActivity.java

 1 package com.example.app02;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.annotation.SuppressLint;
 6 import android.content.Intent;
 7 import android.os.Bundle;
 8 import android.view.View;
 9 import android.widget.Button;
10 
11 public class MainActivity extends AppCompatActivity {
12 //先声明空间
13     private Button mBtnTextView;
14     private Button mBtnButton;
15     private Button mBtnEditText;
16     @SuppressLint("MissingInflatedId")
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21         mBtnTextView = findViewById(R.id.btn_textview);
22         mBtnTextView.setOnClickListener(new View.OnClickListener() {
23             @Override
24             public void onClick(View view) {
25 //                跳转到TextView演示界面
26                 Intent intent = new Intent(MainActivity.this, TextViewActivity.class);
27                 startActivity(intent);
28             }
29         });
30         mBtnButton= findViewById(R.id.btn_button);
31         mBtnButton.setOnClickListener(new View.OnClickListener() {
32             @Override
33             public void onClick(View view) {
34 //                 跳转到Button演示界面
35                 Intent intent=new Intent(MainActivity.this,ButtonActivity.class);
36                 startActivity(intent);
37             }
38         });
39         mBtnEditText=findViewById(R.id.btn_edittext);
40         mBtnEditText.setOnClickListener(new View.OnClickListener() {
41             @Override
42             public void onClick(View view) {
43 //                跳转到EditView演示界面
44                 Intent intent=new Intent(MainActivity.this,EditTextActivity.class);
45                 startActivity(intent);
46             }
47         });
48     }
49 }
View Code

EditTextActivity.java

 1 package com.example.app02;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.os.Bundle;
 6 import android.text.Editable;
 7 import android.text.TextWatcher;
 8 import android.util.Log;
 9 import android.view.View;
10 import android.widget.Button;
11 import android.widget.EditText;
12 import android.widget.Toast;
13 
14 public class EditTextActivity extends AppCompatActivity {
15     private Button mBtnLogin;
16     private EditText mEtUserName;
17 
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_edit_text);
22         mBtnLogin=findViewById(R.id.btn_login);
23         mBtnLogin.setOnClickListener(new View.OnClickListener() {
24             @Override
25             public void onClick(View view) {
26                 Toast.makeText(EditTextActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
27             }
28         });
29         mEtUserName=findViewById(R.id.et_1);
30         mEtUserName.addTextChangedListener(new TextWatcher() {
31             @Override
32             public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
33 
34             }
35 
36             @Override
37             public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
38                 Log.d("edittext", charSequence.toString());
39 
40             }
41 
42             @Override
43             public void afterTextChanged(Editable editable) {
44 
45             }
46         });
47     }
48 }
View Code

主要部分

activity_edit_text.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:padding="15dp">
 6     <EditText
 7         android:id="@+id/et_1"
 8         android:layout_width="match_parent"
 9         android:layout_height="50dp"
10         android:textSize="20sp"
11         android:textColor="#FF0000"
12         android:hint="用户名"
13         android:background="@drawable/bg_et1"
14         android:paddingLeft="10dp"
15         android:paddingRight="10dp"
16         android:drawableLeft="@drawable/username"
17         android:drawablePadding="5dp"
18         android:maxLines="1"
19         android:layout_marginTop="70dp"/>
20     <EditText
21         android:id="@+id/et_2"
22         android:layout_width="match_parent"
23         android:layout_height="50dp"
24         android:layout_below="@id/et_1"
25         android:textSize="20sp"
26         android:hint="密码"
27         android:inputType="textPassword"
28         android:textColor="#FF0000"
29         android:layout_marginTop="15dp"
30         android:background="@drawable/bg_et1"
31         android:paddingLeft="10dp"
32         android:paddingRight="10dp"
33         android:drawableLeft="@drawable/password"
34         android:drawablePadding="5dp"
35         android:maxLines="1"/>
36 <!--登录按钮-->
37     <Button
38         android:id="@+id/btn_login"
39         android:layout_width="match_parent"
40         android:layout_height="40dp"
41         android:layout_below="@id/et_2"
42         android:layout_marginTop="30dp"
43         android:background="@drawable/bg_btn4"
44         android:text="登录"
45         android:textColor="#fff"
46         android:textSize="20sp"/>
47 </RelativeLayout>
View Code

另外:可能会出现找到的图片尺寸不合适,这里直接使用画图工具改变尺寸大小就可以了!

posted @ 2023-01-06 16:27  喝着农药吐泡泡o  阅读(13)  评论(0编辑  收藏  举报