MVP框架做一个简单的登录

Main_layout
<LinearLayout
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp">

    <EditText
        android:layout_margin="5dp"
        android:id="@+id/edit_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入用户名"/>

    <EditText
        android:layout_margin="5dp"
        android:id="@+id/edit_pwd"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:password="true"
        android:hint="请输入密码"/>

    <RelativeLayout
        android:layout_margin="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <CheckBox
            android:id="@+id/cb_remember"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="记住密码"
            />
        <CheckBox
            android:id="@+id/cb_auto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="自动登录"
            android:layout_alignParentRight="true"
            />
    </RelativeLayout>

    <Button
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录"
        android:textColor="#fff"
        android:textSize="20sp"
        android:layout_margin="20dp"
        android:background="@drawable/btn_style"/>


</LinearLayout>

 

MainActivity:

public class MainActivity extends AppCompatActivity implements IntView{

    private EditText mIdd;
    private EditText mUsername;
    private Button mLogin;
    private CheckBox mNumber_password;
    private CheckBox mAutomatic;

    private UserPersenter mUserPersenter;

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

        //用P层里面的方法
        mUserPersenter = new UserPersenter();
        mUserPersenter.onAttach(this);
        mUserPersenter.setValue();
    }


    private void initview() {
        mIdd = findViewById(R.id.idd);
        mUsername = findViewById(R.id.username);
        mLogin = findViewById(R.id.login);
        mNumber_password = findViewById(R.id.number_password);
        mAutomatic = findViewById(R.id.automatic);
    }


    //实现IntView接口 然后里面就是前面所传过来的值,就可以直接使用了
    @Override
    public void Valueset(final Userbean.DataBean dataBean) {
        mLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
		mTname = editName.getText().toString().trim);
		mTpwd = editPwd.getText().toString().trim();
                long mobile = dataBean.getMobile();
                String s = String.valueOf(mobile);
                if(mTname.equals(dataBean.getName()) && mTpwd.equals(s)){
                     Toast.makeText(MainActivity.this, "成功", Toast.LENGTH_SHORT).show();
                }else{
                     Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //这就是调用P层里面的为了防止内存泄漏而调用的方法
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUserPersenter.onDestroy();
    }
}

记住密码:点击按钮判断这个按钮是否是选中状态返回不同的布尔值  使用Sharedferences

if (cbRemember.isChecked()){
    mEdit.putBoolean("remember",true);
    mEdit.putString("tname",mTname);
    mEdit.putString("tpwd",mTpwd);
    mEdit.commit();
}else {
    mEdit.putBoolean("remember",false);
    mEdit.putString("tname","");
    mEdit.putString("tpwd","");
    mEdit.commit();
}

 

sp = getSharedPreferences("config", MODE_PRIVATE);
mEdit = sp.edit();
boolean bt = sp.getBoolean("remember", false);
String tname = sp.getString("tname", "");
String tpwd = sp.getString("tpwd", "");
if (bt){
    editName.setText(tname);
    editPwd.setText(tpwd);
    cbRemember.setChecked(true);
}
if (sp.getBoolean("auto",false)){
    cbAuto.setChecked(true);
    Intent intent = new Intent(MainActivity.this,ShowActivity.class);
    intent.putExtra("name", tname);
    startActivity(intent);
}

自动登录:判断返回的布尔值:判断选择框的状态
 

if (sp.getBoolean("auto",false)){
    cbAuto.setChecked(true);
    Intent intent = new Intent(MainActivity.this,ShowActivity.class);
    intent.putExtra("name", tname);
    startActivity(intent);
}
cbAuto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (cbAuto.isChecked()){
            mEdit.putBoolean("auto",true).commit();
        }else {
            mEdit.putBoolean("auto",false).commit();
        }
    }
});

Presenter:

public class UserPersenter {
    private IntView mIntView;
    private String path="http://www.xieast.com/api/user/login.php?username=13800138000&password=123456";
    public void setValue(){

        UserModel userModel = new UserModel();
        userModel.getData(path);



        //调用P层里面的接口回调获取到的值
        userModel.setOnSetChangeValue(new UserModel.OnSetChangeValue() {
            @Override
            public void getdata(Userbean.DataBean list) {
                //然后在吧P层的值给IntView接口
                mIntView.Valueset(list);
            }
        });
    }

    //这个方法是手写的,就是让主页面调用本界面传值。。
    public void onAttach(IntView mIntView){
        this.mIntView=mIntView;
    }

    //这个方法也是手写的,就是为了防止内存泄漏,在主页面调用OnDestroy方法然后调用这个方法,就可以把接口释放
    public void onDestroy(){
        if(mIntView!=null){
            mIntView=null;
        }
    }

}

MOdel:

public class UserModel {


    //写一个接口回调供P层获取到本类的bean结果,
    public interface OnSetChangeValue{
        void getdata(Userbean.DataBean list);
    }

    private OnSetChangeValue mOnSetChangeValue;

    public void setOnSetChangeValue(OnSetChangeValue onSetChangeValue){
        mOnSetChangeValue=onSetChangeValue;
    }


    //
    public void getData(String path){
        new AsyncTask<String, Void, Userbean.DataBean>() {
            @Override
            protected Userbean.DataBean doInBackground(String... strings) {

                String gsonString =HttpUtils.HttpUtilsConnection(strings[0]);
                Userbean gson  = new Gson().fromJson(gsonString,Userbean.class);

                //把获取到的值供给P层
                mOnSetChangeValue.getdata(gson.getData());
                return null;
            }

        }.execute(path);
    }
}

intView:

public interface IntView {
    //获取到接口里面的值的话,然后让主页面实现该接口,就可以获取到从P里面的Model层里面的值,
    void Valueset(Userbean.DataBean dataBean);
}

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值