学习记录(3.16)

  学习时长:6h

  代码行数:约294行

  今天主要完成了登陆界面的优化,可以在一个页面进入账号密码登录和验证码登录两种登陆方式,并且可以独立显示和进行。在此之后完成了忘记密码后的密码重置。

  因为今天要给社团中的大一新生上乐理课,所以还制作了相应的ppt占用了不少时间,所以并没有编写太多代码,明天继续加油

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <RadioGroup
        android:id="@+id/rg_Login"
        android:layout_width="match_parent"
        android:layout_height="@dimen/item_layout_height"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/rb_password"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="密码登录"
            android:textSize="@dimen/common_font_size"
            android:checked="true"/>
        <RadioButton
            android:id="@+id/rb_verifycode"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="验证码登录"
            android:textSize="@dimen/common_font_size"/>
    </RadioGroup>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="@dimen/item_layout_height"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="手机号"
            android:textSize="30dp"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/a_phone"
            android:hint="请输入手机号"
            android:inputType="number"/>

    </LinearLayout>
   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="@dimen/item_layout_height"
       android:orientation="horizontal">
        <TextView
            android:id="@+id/tv_password"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="密    码"
            android:textSize="30dp"/>
       <RelativeLayout
           android:layout_width="0dp"
           android:layout_height="match_parent"
           android:layout_weight="1">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:hint="请输入密码"
                android:id="@+id/a_password"
                android:inputType="textPassword"/>
           <Button
               android:id="@+id/btn_forget"
               android:layout_width="wrap_content"
               android:layout_height="match_parent"
               android:layout_alignParentEnd="true"
               android:text="忘记密码"
               android:background="@color/teal_200"/>
       </RelativeLayout>

   </LinearLayout>
    <CheckBox
        android:id="@+id/ck_remember"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="保存密码"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <Button
            android:id="@+id/iv_denglu"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="登录"
            android:textSize="40dp"
            android:background="@color/teal_200"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="       "/>
        <Button
            android:id="@+id/zhuceb"
            android:layout_width="200dp"
            android:layout_height="wrap_content"
            android:text="注册"
            android:textSize="40dp"
            android:background="@color/teal_200"/>
    </LinearLayout>
</LinearLayout>

  

package com.example.myapplication1;

import static com.example.myapplication1.R.id.a_password;
import static com.example.myapplication1.R.id.iv_denglu;
import static com.example.myapplication1.R.id.rg_Login;
import static com.example.myapplication1.R.id.tv_password;
import static com.example.myapplication1.R.id.zc_phone;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.internal.ViewUtils;

import java.text.BreakIterator;
import java.util.Random;
import java.util.concurrent.atomic.AtomicReference;
public class Main3Activity extends AppCompatActivity implements View.OnFocusChangeListener, RadioGroup.OnCheckedChangeListener, View.OnClickListener {

    private EditText a_phone;
    private TextView tv_password;
    private EditText a_password;
    private Button btn_forget;
    private CheckBox ck_remember;
    private RadioButton rb_password;
    private RadioButton rb_verifycode;
    private ActivityResultLauncher<Intent> regist;
    private Button iv_denglu;
    private String mpassword="20214121";
    private String verifyCode;
    private SharedPreferences preferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        RadioGroup rg_login = findViewById(rg_Login);
        tv_password = findViewById(R.id.tv_password);
        a_password = findViewById(R.id.a_password);
        btn_forget = findViewById(R.id.btn_forget);
        ck_remember = findViewById(R.id.ck_remember);
        rb_password = findViewById(R.id.rb_password);
        rb_verifycode = findViewById(R.id.rb_verifycode);
        rg_login.setOnCheckedChangeListener(this);
        btn_forget.setOnClickListener(this);
        regist = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                Intent intent =result.getData();
                if(intent!=null && result.getResultCode() == Activity.RESULT_OK){
                    mpassword=intent.getStringExtra("new_password");
                }
            }
        });

        a_phone = findViewById(R.id.a_phone);
        a_password.setOnFocusChangeListener(this);
        Button button = findViewById(R.id.zhuceb);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(Main3Activity.this, Zhuce.class);
                startActivity(intent);
            }
        });
        preferences = getSharedPreferences("confine", Context.MODE_PRIVATE);
        iv_denglu = findViewById(R.id.iv_denglu);
        iv_denglu.setOnClickListener(this);
        reload();
    }
    public void reload(){
        boolean isremember = preferences.getBoolean("remember",false);
        if(isremember){
            String phone = preferences.getString("phone","");
            a_phone.setText(phone);
            String password = preferences.getString("password","");
            a_password.setText(password);
        }
    }
    @Override
    public void onFocusChange(View view, boolean b) {
        if(b){

            String phone = a_phone.getText().toString();
            if(TextUtils.isEmpty(phone) || phone.length()<11){
                a_phone.requestFocus();
                Toast.makeText(this,"请输入11位手机号码", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        switch (i){
            case R.id.rb_password:
                tv_password.setText("密  码");
                a_password.setText("请输入六位密码");
                btn_forget.setText("忘记密码");
                ck_remember.setVisibility(View.VISIBLE);
                break;
            case R.id.rb_verifycode:
                tv_password.setText("验证码");
                a_password.setText("请输入验证码");
                btn_forget.setText("获取验证码");
                ck_remember.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void onClick(View view) {
        String phone= a_phone.getText().toString();
        switch (view.getId()){
            case R.id.btn_forget:
                if(phone.length()<11){
                    Toast.makeText(this,"请输入11位手机号",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(rb_password.isChecked()){
                    Intent intent = new Intent(this, Forget.class);
                    intent.putExtra("phone", phone);
                    regist.launch(intent);
                }
                else if(rb_verifycode.isChecked()){
                    verifyCode = String.format("%06d",new Random().nextInt(999999));
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("请记住验证码");
                    builder.setMessage("尊敬的用户"+phone+"您本次的验证码为"+ verifyCode);
                    builder.setPositiveButton("确认",null);
                    AlertDialog dialog = builder.create();
                    dialog.show();
                }
                break;
            case R.id.iv_denglu:
                if(rb_password.isChecked()){
                    if(!mpassword.equals(a_password.getText().toString())){
                        Toast.makeText(this,"请输入正确的密码",Toast.LENGTH_SHORT).show();
                        return;
                    }
                    loginSuccess();
                }
                else if(rb_verifycode.isChecked()){
                   if(!verifyCode.equals(a_password.getText().toString())){
                       Toast.makeText(this,"请输入正确的验证码",Toast.LENGTH_SHORT).show();
                       return;
                   }
                   loginSuccess();
                }
        }
    }

    private void loginSuccess() {
        Intent intent = new Intent();
        intent.setClass(Main3Activity.this, Menu.class);
        startActivity(intent);

        if(ck_remember.isChecked()) {
            SharedPreferences.Editor editor = preferences.edit();
            editor.putString("phone",a_phone.getText().toString());
            editor.putString("password",a_password.getText().toString());
            editor.putBoolean("remember",ck_remember.isChecked());
            editor.commit();
        }
    }
}

  

 

posted @ 2023-03-16 21:14  霍普金斯大学丁真  阅读(15)  评论(0)    收藏  举报