MVP之登录和注册

来源:互联网 发布:动物拍照软件下载 编辑:程序博客网 时间:2024/05/04 04:22


1.model 包 2. presenter  包 3、utils 4.view


utils下都是我们要用的一些工具类,我的博客里也有那些工具类,在这里就不一一写出来了
view包下放的还有我们的  adapter   、 horder


     view下 activity包下  登录Mainactivity

记得要初始化 

Application

public class MainActivity extends AppCompatActivity implements IDengLu{    private EditText tel;    private EditText pass;    private DengLuPersenter dengLuPersenter;    private SharedPreferences aa;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tel = (EditText) findViewById(R.id.tel);        pass = (EditText) findViewById(R.id.pass);        dengLuPersenter = new DengLuPersenter(this);
 aa = getSharedPreferences("aa", MODE_PRIVATE);     boolean b = aa.getBoolean("is", false);        if (b){            Intent intent=new Intent(MainActivity.this,Main3Activity.class);            startActivity(intent);        }    }    public void zhuce(View view) {        Intent intent=new Intent(MainActivity.this,ZhuCeActivity.class);        startActivity(intent);    }    public void denglu(View view) {        String t = tel.getText().toString();        String s = pass.getText().toString();        dengLuPersenter.dengluUser(t,s,ApiUtil.DENGLU);    }    @Override    public void onSuccess(final DengLubean dengLubean) {        runOnUiThread(new Runnable() {            @Override            public void run() {                String s = dengLubean.getCode().toString();                if("0".equals(s))                {                    Toast.makeText(MainActivity.this,"登陆成功",Toast.LENGTH_SHORT).show();                           String t = tel.getText().toString();                    String p = pwd.getText().toString();                    SharedPreferences.Editor edit = aa.edit();                    edit.putString("mobile",t);                    edit.putString("password",p);                    edit.putBoolean("is",true);                    edit.commit();                    Intent intent=new Intent(MainActivity.this,Main3Activity.class);                    startActivity(intent);                }                else                {                    Toast.makeText(MainActivity.this,dengLubean.getMsg().toString(),Toast.LENGTH_SHORT).show();                }            }        });    }    @Override    public void onError() {    }}
  view下 activity包下  注册Mainactivity
public class ZhuCeActivity extends AppCompatActivity implements Izhuce{    private EditText zctel;    private EditText zctel1;    private EditText zcpass;    private ZhuCePresenter zhuCePresenter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_zhu_ce);        zctel1 = (EditText) findViewById(R.id.zctel);        zcpass = (EditText) findViewById(R.id.zcpass);        zhuCePresenter = new ZhuCePresenter(this);        Log.d("tttt",ApiUtil.ZHUCE);    }    public void zczhuce(View view) {        String tel = zctel1.getText().toString();        String pass = zcpass.getText().toString();        Log.d("tttt",ApiUtil.ZHUCE);        if(!TextUtils.isEmpty(tel)&&!TextUtils.isEmpty(pass)){            if(isPhoneNumber(tel)&&pass.length()>=6){                zhuCePresenter.zhuceUser(tel,pass,"http://120.27.23.105/user/reg");            }else if(!isPhoneNumber(tel))            {                Toast.makeText(ZhuCeActivity.this,"请输入正确的手机号",Toast.LENGTH_SHORT).show();            }else if(pass.length()<6)            {                Toast.makeText(ZhuCeActivity.this,"密码长度至少六位",Toast.LENGTH_SHORT).show();            }        }else if(TextUtils.isEmpty(tel))        {            Toast.makeText(ZhuCeActivity.this,"手机号不能为空",Toast.LENGTH_SHORT).show();        }else if(TextUtils.isEmpty(pass))        {            Toast.makeText(ZhuCeActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();        }    }    //判断是否是手机号    public static boolean isPhoneNumber(String phoneNo) {        if (TextUtils.isEmpty(phoneNo)) {            return false;        }        if (phoneNo.length() == 11) {            for (int i = 0; i < 11; i++) {                if (!PhoneNumberUtils.isISODigit(phoneNo.charAt(i))) {                    return false;                }            }            Pattern p = Pattern.compile("^((13[^4,\\D])" + "|(134[^9,\\D])" +                    "|(14[5,7])" +                    "|(15[^4,\\D])" +                    "|(17[3,6-8])" +                    "|(18[0-9]))\\d{8}$");            Matcher m = p.matcher(phoneNo);            return m.matches();        }        return false;    }    @Override    public void onSuccess(final ZhuCeBean zhuCeBean) {        runOnUiThread(new Runnable() {            @Override            public void run() {                String code = zhuCeBean.getCode();                if("0".equals(code)){                    Toast.makeText(ZhuCeActivity.this,"注册成功,请登录",Toast.LENGTH_SHORT).show();                    ZhuCeActivity.this.finish();                }else                {                    Toast.makeText(ZhuCeActivity.this,zhuCeBean.getMsg(),Toast.LENGTH_SHORT).show();                }            }        });    }    @Override    public void onError() {    }    public void fanhui(View view) {        ZhuCeActivity.this.finish();    }}

   view下 Iview包下  接口登录

public interface IDengLu {    void onSuccess(DengLubean dengLubean);    void onError();}

 view下 Iview包下  接口注册
public interface Izhuce {    void onSuccess(ZhuCeBean zhuCeBean);    void onError();} 

   presenter包下    登录

public class DengLuPersenter implements IDengLuP{    private  IDengLu iDengLu;    private  DengLuModel dengLuModel;    public DengLuPersenter(IDengLu iDengLu) {        dengLuModel = new DengLuModel(this);        this.iDengLu=iDengLu;    }    public void dengluUser(String tel,String pass,String url){        dengLuModel.dengluUser(tel,pass,url);    }    @Override    public void onSuccessP(DengLubean dengLubean) {        iDengLu.onSuccess(dengLubean);    }    @Override    public void onErrorP() {    }}
   presenter包下    注册

public class ZhuCePresenter implements IZhuCeP{    private  Izhuce izhuce;    private  ZhuCeModel zhuCeModel;    public ZhuCePresenter(Izhuce izhuce) {        this.izhuce=izhuce;        zhuCeModel = new ZhuCeModel(this);    }        public void zhuceUser(String phone,String encodePwd,String registUrl)        {            zhuCeModel.zhuceUser(phone,encodePwd,registUrl);        }    @Override    public void onZhuCeSuccess(ZhuCeBean zhuCeBean) {        izhuce.onSuccess(zhuCeBean);    }    @Override    public void onZhuCeError() {    }}
 presenter包下   inter包下  接口登录

public class DengLuModel {    private  IDengLuP iDengLuP;    public DengLuModel(IDengLuP iDengLuP) {        this.iDengLuP=iDengLuP;    }    public void dengluUser(String tel,String pass,String url){        Map<String, String> params = new HashMap<>();        params.put("mobile",tel);        params.put("password",pass);        OkHttp3Util.doPost(url, params, new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                String json = response.body().string();                DengLubean dengLubean = new Gson().fromJson(json, DengLubean.class);                iDengLuP.onSuccessP(dengLubean);            }        });    }}
presenter包下   inter包下  接口登录

public interface IZhuCeP {    void onZhuCeSuccess(ZhuCeBean zhuCeBean);    void onZhuCeError();}

   model包下   登录

public class DengLuModel {    private  IDengLuP iDengLuP;    public DengLuModel(IDengLuP iDengLuP) {        this.iDengLuP=iDengLuP;    }    public void dengluUser(String tel,String pass,String url){        Map<String, String> params = new HashMap<>();        params.put("mobile",tel);        params.put("password",pass);        OkHttp3Util.doPost(url, params, new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                String json = response.body().string();                DengLubean dengLubean = new Gson().fromJson(json, DengLubean.class);                iDengLuP.onSuccessP(dengLubean);            }        });    }}

   model包下   注册

public class ZhuCeModel {    private  IZhuCeP iZhuCeP;    public ZhuCeModel(IZhuCeP iZhuCeP) {        this.iZhuCeP=iZhuCeP;    }    public void zhuceUser(String phone,String encodePwd,String zhuceUrl){        OkHttpClient okHttpClient=new OkHttpClient();        RequestBody formBody = new FormBody.Builder()                .add("mobile", phone)                .add("password", encodePwd)                .build();        Request request = new Request.Builder()                .post(formBody)                .url(zhuceUrl)                .build();        okHttpClient.newCall(request).enqueue(new Callback() {            @Override            public void onFailure(Call call, IOException e) {            }            @Override            public void onResponse(Call call, Response response) throws IOException {                if(response.isSuccessful()){                    String json = response.body().string();                    ZhuCeBean zhuCeBean = new Gson().fromJson(json, ZhuCeBean.class);                    iZhuCeP.onZhuCeSuccess(zhuCeBean);                }            }        });    }}
     model包下   bean包下的,你们应该知道这是什么


  登录的布局

<LinearLayout    android:orientation="vertical"    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" tools:context="bwie.com.zhoukao2.view.activity.MainActivity">    <TextView        android:gravity="center"        android:textSize="30dp"        android:text="登录"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <EditText        android:id="@+id/tel"        android:layout_marginTop="20dp"        android:hint="请输入手机号"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <EditText        android:id="@+id/pass"        android:layout_marginTop="20dp"        android:hint="请输入密码"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <LinearLayout        android:gravity="center"        android:layout_marginTop="20dp"        android:orientation="horizontal"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:onClick="denglu"            android:text="登录"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <Button            android:onClick="zhuce"            android:text="注册"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />    </LinearLayout></LinearLayout>

注册的布局

<LinearLayout    android:orientation="vertical"    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"    tools:context="bwie.com.zhoukao2.view.activity.ZhuCeActivity">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:onClick="fanhui"            android:gravity="center"            android:text="返回"            android:layout_width="wrap_content"            android:layout_height="wrap_content" />        <TextView            android:gravity="center"            android:text="注册"            android:textSize="30dp"            android:layout_width="match_parent"            android:layout_height="wrap_content" />    </LinearLayout>    <EditText        android:id="@+id/zctel"        android:layout_marginTop="20dp"        android:hint="请输入手机号"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <EditText        android:id="@+id/zcpass"        android:layout_marginTop="20dp"        android:hint="请输入密码"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <Button        android:text="注册"        android:layout_marginTop="20dp"        android:onClick="zczhuce"        android:layout_gravity="center"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

原创粉丝点击