本地登录实现

MainActivity

package example.mytestdemo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    Intent intent;
    private String passwordStr;
    private String accountStr;
    EditText password;
    EditText account;
    Button button;
    String na="admin";
    String pa="123456";
    CheckBox showPassword;
    Boolean flag;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button=findViewById(R.id.button);
        password=findViewById(R.id.password);
        account=findViewById(R.id.account);
        showPassword=findViewById(R.id.show_password);
        password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                passwordStr=(password.getText().toString());
            }
        });
        account.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                accountStr=(account.getText().toString());
               
            }
        });

        showPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                
                if (b) {
                    //如果选中,显示密码
                    password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    //否则隐藏密码
                    password.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }


            }
        });

        intent=new Intent(MainActivity.this,success_log_in.class);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(passwordStr.equals(pa)&&accountStr.equals(na)){
                    Toast.makeText(MainActivity.this, accountStr+"登录成功", Toast.LENGTH_SHORT).show();
                    startActivity(intent);
                }
                else{
                    Toast.makeText(MainActivity.this,"账号或密码错误",Toast.LENGTH_SHORT).show();
                }
            }
        });

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:background="@drawable/background">
    <ImageView
        android:layout_marginTop="100dp"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/bussy"/>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="90dp">
        <ImageView
            android:layout_marginTop="25dp"
            android:layout_width="50dp"
            android:layout_height="60dp"
            android:src="@drawable/denglubiao"
            />

        <EditText
            android:id="@+id/account"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:layout_marginLeft="60dp"
            android:layout_marginTop="30dp"
            android:hint="账号"
            android:textStyle="bold"
            android:inputType="text"
            android:maxEms="11"
            android:minEms="7"
            android:textSize="22sp" />
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="90dp">
        <ImageView
            android:layout_marginTop="25dp"
            android:layout_width="50dp"
            android:layout_height="60dp"
            android:src="@drawable/password"
            />
        <EditText
            android:id="@+id/password"
            android:maxLength="16"
            android:inputType="textPassword"
            android:layout_marginLeft="60dp"
            android:layout_marginTop="30dp"
            android:layout_width="300dp"
            android:layout_height="50dp"
            android:hint="密码"
            android:textStyle="bold"
            android:textSize="22sp"/>
    </RelativeLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="15dp">
        <CheckBox
            android:layout_marginLeft="50dp"
            android:id="@+id/show_password"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="显示密码"
            android:textSize="15sp"
            />

        <Button
            android:id="@+id/forget_password"
            android:layout_width="100dp"
            android:layout_height="35dp"
            android:layout_alignTop="@id/show_password"
            android:background="@drawable/tou"
            android:layout_marginLeft="100dp"
            android:layout_toRightOf="@id/show_password"
            android:text="忘记密码"
            android:textSize="15sp" />
    </RelativeLayout>

    <Button
        android:id="@+id/button"
        android:layout_marginTop="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textSize="30sp"
        android:layout_gravity="center"
        android:background="#1D86DA"/>

</LinearLayout>
在界面间跳转使用了intent
Intent intent=new Intent(MainActivity.this,success_log_in.class);

Intent在Android开发中被誉为“意图”,从字面意思不难理解,就是“你打算去哪”

使用了大量的事件监听

其中EditText的.getText();

只能在监听事件中使用

用Toast.makeText(MainActivity.this, String, Toast.LENGTH_SHORT).show();

进行了提示

使用

if (b) {
    //如果选中,显示密码
    password.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
} else {
    //否则隐藏密码
    password.setTransformationMethod(PasswordTransformationMethod.getInstance());
}

实现了密码的可视化与*间的转换一开始使用

password.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD);

发现显示密码后再次点击CheckBox不会变为*

password>setSelection(位置);

password>setSelection(passwordStr.length());

解决点击CheckBox后光标移动到最前方

获取EditText内容的两种方法

 password.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                passwordStr=(password.getText().toString());
            }
        });
        account.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                accountStr=(account.getText().toString());

            }
        });

但是该方法获取值需要在输入完成后点击enter才能读取到值不够方便所以使用change监听addTextChangedListener()

account.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //@在编辑框改变前调用
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // @编辑框改变的时候进行调用

            }

            @Override
            public void afterTextChanged(Editable s) {
                // @编辑框改变之后进行调用
                accountStr=(account.getText().toString());
            }
        });

        password.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //@在编辑框改变前调用
            }
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
               // @编辑框改变的时候进行调用
                //password.setText(s.toString());
            }

            @Override
            public void afterTextChanged(Editable s) {
               // @编辑框改变之后进行调用
                passwordStr=(password.getText().toString());

            }
        });

这种方法就无需点击enter即可获取数值非常方便

关于打包APK方法

点击导航栏的Build→Generate Signed Bundle / APK…>

最后选择release

点击Finish出现

就完成了(project观察模式找的比较快)

在module的release中就可找到APK复制发送到手机就行了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值