MVP登录

这里是引用

布局

<?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=".MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="center"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginBottom="20dp">
        <TextView
            android:layout_marginLeft="30dp"
            android:layout_width="30dp"
            android:layout_height="50dp"
            android:text="用户名"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/name"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_marginLeft="30dp"
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            android:text="密码"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/password"/>
    </LinearLayout>
    <CheckBox
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:id="@+id/jizhu"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/denglu"
        android:text="登录"/>


</LinearLayout>

modle层

public class LoginBiz {
    public void login(String name, String password, CallBack callBack){

        if (name.equals("123")&&password.equals("123")){

            callBack.onSussecc("登录成功");
        }else{
            callBack.onFailre("登录失败");
        }
    }

}

presenter层

public class LoginPresenter {
    private LoginView mloginView;
    private final LoginBiz loginBiz;

    public LoginPresenter(LoginView mloginView) {
        this.mloginView = mloginView;
        loginBiz = new LoginBiz();
    }
    public void login(String name,String password){

        mloginView.show();
        loginBiz.login(name, password, new CallBack() {
            @Override
            public void onSussecc(String surl) {

                mloginView.success(surl);
                mloginView.show();
            }

            @Override
            public void onFailre(String surl) {

      `在这里插入代码片`          mloginView.failer(surl);
                mloginView.hide();
            }
        });
    }
}

view层

public interface LoginView {
    void success(String result);
    void failer(String result);

CallBack类

public interface CallBack {
    void onSussecc(String surl);
    void onFailre(String surl);
}

MainActivity

public class MainActivity extends AppCompatActivity implements LoginView{
    private CheckBox jizhu;
    private Button denglu;
    private EditText password;
    private EditText name;
    private LoginPresenter loginPresenter;
    private SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loginPresenter = new LoginPresenter(this);
        //获取控件
        initView();
        //获取数据
        initDate();
    }

    private void initDate() {
        sharedPreferences = getSharedPreferences("login", MODE_PRIVATE);
        denglu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(MainActivity.this, name.getText().toString().trim() + password.getText().toString().trim(), Toast.LENGTH_SHORT).show();
                loginPresenter.login(name.getText().toString().trim(), password.getText().toString().trim());
            }
        });
        //判断
        if (sharedPreferences.getBoolean("记住密码", false)) {
            String sname = sharedPreferences.getString("用户名", "");
            String spassword = sharedPreferences.getString("密码", "");
            boolean b = sharedPreferences.getBoolean("记住密码", false);
            jizhu.setChecked(b);
            name.setText(sname);
            password.setText(spassword);

        }
    }

    private void initView() {
        name = findViewById(R.id.name);
        password = findViewById(R.id.password);
        denglu = findViewById(R.id.denglu);
        jizhu = findViewById(R.id.jizhu);
    }

    @Override
    public void success(String result) {
        Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
        //存值
        if (jizhu.isChecked()) {
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putString("用户名", name.getText().toString().trim());
            edit.putString("密码", password.getText().toString().trim());
            edit.putBoolean("记住密码", true);
            edit.commit();
        }
        Intent intent = new Intent(MainActivity.this, TowActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void failer(String result) {
        Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void show() {

    }

    @Override
    public void hide() {

    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值