欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

登陆注册Mvp

程序员文章站 2022-04-18 18:02:53
...

**

MainActivity页面 登录页面

**

package com.example.y700_15.ds2_zk2.view.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.y700_15.ds2_zk2.R;
import com.example.y700_15.ds2_zk2.model.bean.LoginBean;
import com.example.y700_15.ds2_zk2.presenter.LoginPresenter;
import com.example.y700_15.ds2_zk2.view.interfaces.LoginView;

import java.util.HashMap;

public class MainActivity extends BaseActivity implements LoginView<LoginBean> {
    private EditText edit_name,edit_pwd;
    private CheckBox check_jz;
    private TextView text_reg;
    private Button but_login;

    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private LoginPresenter loginPresenter;
    private HashMap<String,String> params;
    private String urername,password;

    @Override
    protected int setLayout() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        edit_name = findViewById(R.id.edit_name);
        edit_pwd = findViewById(R.id.edit_pwd);
        check_jz = findViewById(R.id.check_jz);
        text_reg = findViewById(R.id.text_reg);
        but_login = findViewById(R.id.but_login);
    }

    @Override
    protected void initData() {


        loginPresenter = new LoginPresenter();
        loginPresenter.setMview(this);

        sp = getSharedPreferences("cofig",MODE_PRIVATE);
        if (sp.getBoolean("isRem",true)){
            check_jz.setChecked(true);
            edit_name.setText(sp.getString("phone",""));
            edit_pwd.setText(sp.getString("pwd",""));
        }

        check_jz.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b){
                    check_jz.setChecked(true);
                }
            }
        });


        but_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                params = new HashMap<>();
                params.put("phone",edit_name.getText().toString().trim());
                params.put("pwd",edit_pwd.getText().toString().trim());
                loginPresenter.getData(params);

                if (check_jz.isChecked()){
                    editor = sp.edit();
                    editor.putString("phone",edit_name.getText().toString().trim());
                    editor.putString("pwd",edit_pwd.getText().toString().trim());
                    editor.putBoolean("isRem",true);
                    editor.commit();
                }else {
                    editor = sp.edit();
                    editor.putString("phone","");
                    editor.putString("pwd","");
                    editor.putBoolean("isRem",false);
                    editor.commit();
                }

            }
        });

        text_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, RegActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void success(LoginBean loginBean) {
        Toast.makeText(MainActivity.this,loginBean.message,Toast.LENGTH_SHORT).show();
        if (loginBean.message.equals("登录成功")){
            Intent intent = new Intent(MainActivity.this,Main2Activity.class);
            startActivity(intent);
            finish();
        }
        return;
    }

    @Override
    public void failure(String msg) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        loginPresenter.detachView();
    }
}

**

登陆页面的布局

**

<?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"
    android:orientation="vertical"
    tools:context=".view.activity.MainActivity">


    <EditText
        android:id="@+id/edit_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="130dp"
        android:hint="手机号"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        />

    <EditText
        android:id="@+id/edit_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="登录密码"
        android:layout_marginTop="20dp"
        android:layout_marginRight="30dp"
        android:layout_marginLeft="30dp"
        android:password="true"
        />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        >

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/check_jz"
            android:text="记住密码"
            />

        <TextView
            android:id="@+id/text_reg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="快速注册"
            android:layout_alignParentRight="true"
            android:layout_marginTop="5dp"
            />

    </RelativeLayout>


    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/but_login"
        android:text="登录"
        android:layout_marginTop="110dp"
        android:layout_marginRight="30dp"
        android:layout_marginLeft="30dp"
        />

</LinearLayout>

**

RegActivity 注册页面

**

package com.example.y700_15.ds2_zk2.view.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.y700_15.ds2_zk2.R;
import com.example.y700_15.ds2_zk2.model.bean.RegBean;
import com.example.y700_15.ds2_zk2.presenter.RegPresenter;
import com.example.y700_15.ds2_zk2.view.interfaces.RegView;

import java.util.HashMap;

public class RegActivity extends AppCompatActivity implements RegView<RegBean> {
    private EditText edit_names,edit_pwds;
    private Button button;
    private TextView textView;

    private RegPresenter regPresenter;
    private HashMap<String,String> params;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);

        initView();
        initData();
    }

    private void initData() {

        regPresenter = new RegPresenter();
        regPresenter.setMview(this);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                params = new HashMap<>();
                params.put("phone",edit_names.getText().toString().trim());
                params.put("pwd",edit_pwds.getText().toString().trim());
                regPresenter.getData(params);
            }
        });

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RegActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });


    }

    private void initView() {
        edit_names = findViewById(R.id.edit_names);
        edit_pwds = findViewById(R.id.edit_pwds);
        button = findViewById(R.id.but_reg);
        textView = findViewById(R.id.text_login);
    }

    @Override
    public void success(RegBean regBean) {
        Toast.makeText(this,regBean.message,Toast.LENGTH_SHORT).show();
        if (regBean.message.equals("注册成功")){
            Intent intent = new Intent(RegActivity.this,MainActivity.class);
            startActivity(intent);
            finish();
        }

    }

    @Override
    public void failure(String msg) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }


}

<?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"
    android:orientation="vertical"
    tools:context=".view.activity.RegActivity">


    <EditText
        android:id="@+id/edit_names"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="130dp"
        android:hint="手机号"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        />

    <EditText
        android:id="@+id/edit_pwds"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="30dp"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="20dp"
        android:hint="登录密码"
        android:password="true"
        />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
        >

        <TextView
            android:id="@+id/text_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="已有账户?立即登录"
            android:layout_alignParentRight="true"
            />

    </RelativeLayout>

    <Button
        android:id="@+id/but_reg"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginRight="30dp"
        android:layout_marginLeft="30dp"
        android:layout_marginTop="110dp"
        android:text="注册"
        />

</LinearLayout>