学习记录(3.30)

  学习时长:7h

  代码行数:约300行

  今天上午进行了网络课和工程数学课的学习,工程数学课依然是云里雾里,好多概念怎么学呢?哭

  下午体育课篮球赛把对面组干烂了,既挥洒了汗水,又获得了胜利的喜悦,于是乎一高兴就把剩下的app给写完了。

<?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"
    android:gravity="center">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:id="@+id/zhucet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="注册"
            android:textSize="50dp" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="选择您的性别"/>
        <RadioGroup
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="男" />
            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="女"/>
        </RadioGroup>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/zc_id"
            android:hint="请输入用户名"
            android:inputType="text"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/zc_phone"
            android:hint="请输入手机号"
            android:inputType="text"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/zc_password"
            android:hint="请输入密码"
            android:inputType="textPassword"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="确认密码"
            android:inputType="textPassword"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center">
        <Button
            android:id="@+id/iv_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="返回"
            android:textSize="40dp" />
        <Button
            android:id="@+id/zhuceb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="注册"
            android:textSize="40dp" />
    </LinearLayout>
</LinearLayout>

  

package com.example.myapplication1;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Zhuce extends AppCompatActivity implements View.OnClickListener {

    private EditText zc_phone;
    private EditText zc_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhuce);
        zc_id = findViewById(R.id.zc_id);
        zc_phone = findViewById(R.id.zc_phone);
        EditText password = findViewById(R.id.zc_password);
        zc_phone.setOnFocusChangeListener(this::onFocusChange);
        password.setOnFocusChangeListener(this::onFocusChange);
        findViewById(R.id.iv_back).setOnClickListener(this::onClick);
        findViewById(R.id.zhuceb).setOnClickListener(this::onClick);

    }
    public void onFocusChange(View v,boolean b)
    {
        if(b){
            String phone=zc_phone.getText().toString();
            String id=zc_id.getText().toString();
            if(TextUtils.isEmpty(phone) || phone.length()<11) {
                zc_phone.requestFocus();
                Toast.makeText(this, "请输入11位手机号", Toast.LENGTH_SHORT).show();
            }
            if(TextUtils.isEmpty(id)) {
                zc_id.requestFocus();
                Toast.makeText(this, "用户名不能为空", Toast.LENGTH_SHORT).show();
            }

        }
    }
    public void onClick(View view) {
        if(view.getId() == R.id.iv_back || view.getId() == R.id.zhuceb)
            finish();
    }
}

  

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-30 20:34  霍普金斯大学丁真  阅读(7)  评论(0)    收藏  举报