MVP加登录注册

本文深入解析MVP(Model-View-Presenter)架构模式,详细介绍各层组件的设计与实现,包括启动页面、登录模块、注册模块等关键部分。通过具体代码示例,展示如何在Android应用中应用MVP模式,实现清晰的职责划分与良好的代码组织。
摘要由CSDN通过智能技术生成

mvp分层

这里在这里插入图片描述是引用

先写启动页面 LoadingActivity

在这里插入代码片public class LoadingActivity extends AppCompatActivity{

    private TextView tvTime;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading);
        initView();
        initData();
    }
int count=3;
    private void initData() {
        TimerTask timerTask = new TimerTask(){

            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        tvTime.setText(count+"s");
                        if (count==0){
                            //页面跳转
                            jumpActivity();
                        }
                    }
                });
                count--;
            }
        };
        Timer timer = new Timer();
        timer.schedule(timerTask,1,1000);
    }

    private void initView() {
        tvTime = findViewById(R.id.tvTime);
    }
    private void jumpActivity(){
        //是否登录
        SharedPreferences sharedPreferences = getSharedPreferences(Constant.APP_CONFIG,MODE_PRIVATE);
        boolean is_login = sharedPreferences.getBoolean(Constant.IS_LOGIN, false);

        if (is_login){
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
        }else{
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
        }
        finish();
    }
}

启动页的布局 加倒计时
xml布局`

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/ivLoading"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@mipmap/bbbb" />

    <TextView
        android:id="@+id/tvTime"
        style="@style/StyleWrapContent"
        android:textSize="@dimen/dimen_common_18sp"
        android:layout_margin="@dimen/dimen_10dp"
        android:padding="@dimen/dimen_10dp"
        android:layout_alignParentRight="true"
        android:textColor="@color/colorWhite"
        android:background="@drawable/shpe"
        android:text="1s" />
</RelativeLayout>

下面引用的布局在
这是style.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
<style name="StyleWrapContent">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
</style>

这是string.xml

<resources>
    <string name="app_name">仿京东</string>

    <!--login-->
    <string name="login">点击登录</string>
    <string name="login_hin">请输入手机号码</string>
</resources>

在color.xml里面自定义颜色

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>

    <!--common color-->
  <color name="colorRed">#ff0000</color>
  <color name="colorWhite">#ffffff</color>

    <!--font color-->
  <color name="fontBlack">#000000</color>
  <color name="fontRed">#ff0000</color>
  <color name="fontWhite">#ffffff</color>
</resources>

下面在values里面创建dimen.xml
里面写宽高

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <dimen name="dimen_1dp">1dp</dimen>
    <dimen name="dimen_10dp">10dp</dimen>
    <dimen name="dimen_20dp">20dp</dimen>
    <dimen name="dimen_30dp">30dp</dimen>

    <dimen name="dimen_common_18sp">18sp</dimen>
    <dimen name="dimen_common_16sp">16sp</dimen>
    <dimen name="dimen_common_20sp">20sp</dimen>
    <dimen name="dimen_common_22sp">22sp</dimen>
</resources>
``
然后再drawable里面创建shpe

在这里插入图片描述
下面开始写 里面的bean
LoginResponse

public class LoginResponse implements Serializable {
    private String age;

    private String appkey;

    private String appsecret;

    private String createtime;

    private String email;

    private String fans;

    private String follow;

    private String gender;

    private String icon;

    private String latitude;

    private String longitude;

    private String mobile;

    private String money;

    private String nickname;

    private String password;

    private String praiseNum;

    private String token;

    private int uid;

    private String userId;

    private String username;
}

RootBean层

public class RootBean implements Serializable{
    private String msg;
    private int code;
    private String data;

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getCode() {
        return code;
    }
    public String getData() {
        return data;
    }
}

下面开始写model层 里面的LoginModule这里
主要写数据

/*
 * 登录
 * */
public class LoginModule {
    public void login(String moblie, String password, final HttpCallBack callBack) {
        final String url = "http://www.zhaoapi.cn/user/login";
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    URL url1 = new URL(url);
                    HttpURLConnection httpURLConnection= (HttpURLConnection) url1.openConnection();
                    httpURLConnection.setConnectTimeout(5000);
                    httpURLConnection.setRequestMethod("GET");
                    if (httpURLConnection.getResponseCode()==200){
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String s = CharStreams.toString(new InputStreamReader(httpURLConnection.getInputStream()));
                        Gson gson = new Gson();
                        RootBean rootBean = gson.fromJson(s, RootBean.class);
                        if (rootBean.getCode()==0){
                            callBack.onSuccess(rootBean.getData());
                        }else{
                            callBack.onFailer(rootBean.getMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        try {
            Thread.sleep(3000);
            if (moblie.equals("123")&& password.equals("123")){
                callBack.onSuccess("登录成功");
            }else{
                callBack.onFailer("登录失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

RegisterModule 注册

/*
 * 注册
 * */
public class RegisterModule {
    public void login(String moblie, String password, final HttpCallBack callBack) {
        final String url = "http://www.zhaoapi.cn/user/login";
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    URL url1 = new URL(url);
                    HttpURLConnection httpURLConnection= (HttpURLConnection) url1.openConnection();
                    httpURLConnection.setConnectTimeout(5000);
                    httpURLConnection.setRequestMethod("GET");
                    if (httpURLConnection.getResponseCode()==200){
                        InputStream inputStream = httpURLConnection.getInputStream();
                        String s = CharStreams.toString(new InputStreamReader(httpURLConnection.getInputStream()));
                        Gson gson = new Gson();
                        RootBean rootBean = gson.fromJson(s, RootBean.class);
                        if (rootBean.getCode()==0){
                            callBack.onSuccess(rootBean.getData());
                        }else{
                            callBack.onFailer(rootBean.getMsg());
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
        try {
            Thread.sleep(3000);
            if (moblie.equals("123")&& password.equals("123")){
                callBack.onSuccess("登录成功");
            }else{
                callBack.onFailer("登录失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

HttpCallBack接口

public interface HttpCallBack {
    void onSuccess(String result);
    void onFailer(String erroMsg);
}

然后写p层 的LoginPresenter主要写逻辑

public class LoginPresenter {

    private LoginView loginView;

    private final LoginModule loginModule;

    public LoginPresenter(LoginView loginView) {
        this.loginView = loginView;
        loginModule = new LoginModule();
    }
    public void login(String moble,String password){
        loginModule.login(moble, password, new HttpCallBack() {
            @Override
            public void onSuccess(String result) {
                loginView.onSuccess(result);
            }

            @Override
            public void onFailer(String erroMsg) {
           loginView.onFailer(erroMsg);
            }
        });
    }
}

Constant接口

public interface Constant {
    String APP_CONFIG="app_config";//sp文件的名字
    String IS_LOGIN="is_login";//判断是否登录
}

view层LoginView

public interface LoginView {
    void onSuccess(String result);
    void onFailer(String erroMsg);
}

然后写登录页面 LoginActivity


/*
 * 登录界面
 * */
public class LoginActivity extends AppCompatActivity implements LoginView {

    private EditText etMobile;
    private EditText etPassword;
    private TextView tvLogin;
    private LoginPresenter loginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
        initViewListener();
        loginPresenter = new LoginPresenter(this);

    }

    private void initViewListener() {
        tvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击登录
                String mobile = etMobile.getText().toString().trim();
                String password = etPassword.getText().toString();
                loginPresenter.login(mobile, password);
            }
        });

    }

    private void initView() {
        etMobile = findViewById(R.id.etMobile);
        etPassword = findViewById(R.id.etPassword);
        tvLogin = findViewById(R.id.tvlogin);
        findViewById(R.id.zhuce1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onSuccess(String result) {
        SharedPreferences sharedPreferences = getSharedPreferences(Constant.APP_CONFIG, MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean(Constant.IS_LOGIN, true);
        edit.commit();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void onFailer(final String erroMsg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(LoginActivity.this, erroMsg, Toast.LENGTH_SHORT).show();
            }
        });
    }
}

注册页面RegisterActivity

/*
* 注册页面
*
* */
public class RegisterActivity extends AppCompatActivity implements LoginView{

    private TextView zhuce;
    private EditText etPassword1;
    private EditText etMobile1;
    private LoginPresenter loginPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initViewListener();
        loginPresenter = new LoginPresenter(this);
    }

    private void initViewListener() {
        zhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //点击注册
                String mobile1 = etMobile1.getText().toString().trim();
                String password1 = etPassword1.getText().toString();
                loginPresenter.login(mobile1,password1);
            }
        });
    }

    private void initView() {
        etMobile1 = findViewById(R.id.etMobile1);
        etPassword1 = findViewById(R.id.etPassword1);
        zhuce = findViewById(R.id.zhuce);
    }

    @Override
    public void onSuccess(String result) {
        SharedPreferences sharedPreferences = getSharedPreferences(Constant.APP_CONFIG, MODE_PRIVATE);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean(Constant.IS_LOGIN,true);
        edit.commit();
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }

    @Override
    public void onFailer(final String erroMsg) {
           runOnUiThread(new Runnable() {
               @Override
               public void run() {
                   Toast.makeText(RegisterActivity.this,erroMsg,Toast.LENGTH_SHORT).show();
               }
           });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值