登陆注册

//使用mvp
view层中的activity

package com.example.week2.View;

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

import com.example.week2.Moudel.bean.LoginBean;
import com.example.week2.Presenter.LoginPst;
import com.example.week2.R;
import com.example.week2.View.inter.LoginMainInterVw;


public class MainActivity extends AppCompatActivity implements LoginMainInterVw {

    private EditText lophone;
    private EditText lopassword;
    private LoginPst loginPst;
    private SharedPreferences sharedPreferences;
    private String lophe;
    private String lopwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sharedPreferences = getSharedPreferences("mydata", MODE_PRIVATE);
        lophone = (EditText) findViewById(R.id.loginphone);
        lopassword = (EditText) findViewById(R.id.loginpassword);
        loginPst = new LoginPst(this);
        boolean issuccess = sharedPreferences.getBoolean("issuccess", false);
        String phone = sharedPreferences.getString("phone", "");
        String password = sharedPreferences.getString("password", "");
        if (issuccess==true){
            loginPst.setdata("http://120.27.23.105/user/login", phone, password);
           /* Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(MainActivity.this,SearchActivity.class);
            startActivity(intent);
            MainActivity.this.finish();*/
        }
    }

    public void skipRegister(View view) {
        Intent intent=new Intent(MainActivity.this,RegisterActivity.class);
        startActivityForResult(intent,100);
    }

    public void Loginyes(View view) {
        lophe = lophone.getText().toString();
        lopwd = lopassword.getText().toString();
        if (lophe !=null&& lophe !=""&& lopwd !=null&& lopwd !=""){
            loginPst.setdata("http://120.27.23.105/user/login", lophe, lopwd);
        }

    }

    @Override
    public void onSuccess(final LoginBean bean) {
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putBoolean("issuccess",true);
        edit.putString("phone",lophe);
        edit.putString("password",lopwd);
        edit.commit();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (bean.getMsg().equals("登录成功")){
                    Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT).show();
                    Intent intent=new Intent(MainActivity.this,SearchActivity.class);
                    startActivity(intent);
                    MainActivity.this.finish();
                }
            }
        });

    }

    @Override
    public void onError() {

    }
}
 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.example.week2.View;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.week2.Moudel.bean.RegisterBean;
import com.example.week2.Presenter.RegisterPst;
import com.example.week2.R;
import com.example.week2.View.inter.RegisterMainInterVw;


public class RegisterActivity extends AppCompatActivity implements RegisterMainInterVw {

    private EditText phone;
    private EditText password;
    private RegisterPst registerPst;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        phone = (EditText) findViewById(R.id.registerphone);
        password = (EditText) findViewById(R.id.registerpassword);
        registerPst = new RegisterPst(this);
    }

    public void Register(View view) {
        String phe = phone.getText().toString();
        String pwd = password.getText().toString();
        phone.setText("");
        password.setText("");
        if (phe!=null&&phe!=""&&pwd!=null&&pwd!=""){
            registerPst.regdata("http://120.27.23.105/user/reg",phe,pwd);
        }
    }

    @Override
    public void onsuccess(final RegisterBean bean) {
           runOnUiThread(new Runnable() {
               @Override
               public void run() {
                   Toast.makeText(RegisterActivity.this,bean.getMsg(),Toast.LENGTH_SHORT).show();
                   if (bean.getMsg().equals("注册成功")){
                       RegisterActivity.this.finish();
                   }
               }
           });
    }

    @Override
    public void onerror() {

    }

    public void fanhui(View view) {
        RegisterActivity.this.finish();
    }
}
view层中的inter接口

package com.example.week2.View.inter;


import com.example.week2.Moudel.bean.LoginBean;

/**
 * Created by FengZhangWei on 2017/12/9.
 */

public interface LoginMainInterVw {
    void onSuccess(LoginBean bean);
    void onError();
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.example.week2.View.inter;

import com.example.week2.Moudel.bean.RegisterBean;

/**
 * Created by FengZhangWei on 2017/12/9.
 */

public interface RegisterMainInterVw {
    void onsuccess(RegisterBean bean);
    void onerror();

}


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Presenter层中接口层

package com.example.week2.Presenter.inter;


import com.example.week2.Moudel.bean.LoginBean;

/**
 * Created by FengZhangWei on 2017/12/9.
 */

public interface LoginInterPst {
    void onSuccess(LoginBean loginBean);
    void onError();
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.example.week2.Presenter.inter;


import com.example.week2.Moudel.bean.RegisterBean;

/**
 * Created by FengZhangWei on 2017/12/9.
 */

public interface RegisterInterPst {
    void onsuccess(RegisterBean bean);
    void onerror();
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Presenter层

package com.example.week2.Presenter;

import com.example.week2.Moudel.LoginMod;
import com.example.week2.Moudel.bean.LoginBean;
import com.example.week2.Presenter.inter.LoginInterPst;
import com.example.week2.View.inter.LoginMainInterVw;

/**
 * Created by FengZhangWei on 2017/12/9.
 */

public class LoginPst implements LoginInterPst {

    private  LoginMod loginMod;
    private  LoginMainInterVw mainInterVw;

    public LoginPst(LoginMainInterVw mainInterVw) {
        this.mainInterVw=mainInterVw;
        loginMod = new LoginMod(this);
    }

    public void setdata(String s, String lophe, String lopwd) {
        loginMod.setdata(s,lophe,lopwd);
    }

    @Override
    public void onSuccess(LoginBean loginBean) {
        mainInterVw.onSuccess(loginBean);
    }

    @Override
    public void onError() {
        mainInterVw.onError();
    }
}
 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.example.week2.Presenter;

import com.example.week2.Moudel.RegisterMod;
import com.example.week2.Moudel.bean.RegisterBean;
import com.example.week2.Presenter.inter.RegisterInterPst;
import com.example.week2.View.inter.RegisterMainInterVw;

/**
 * Created by FengZhangWei on 2017/12/9.
 */

public class RegisterPst implements RegisterInterPst {

    private  RegisterMod registerMod;
    private RegisterMainInterVw mainInterVw;

    public RegisterPst(RegisterMainInterVw mainInterVw) {
        this.mainInterVw=mainInterVw;
        registerMod = new RegisterMod(this);
    }

    public void regdata(String s, String phe, String pwd) {
        registerMod.regdata(s,phe,pwd);
    }

    @Override
    public void onsuccess(RegisterBean bean) {
        mainInterVw.onsuccess(bean);
    }

    @Override
    public void onerror() {

    }
}
moudle层

package com.example.week2.Moudel;

import com.example.week2.Moudel.bean.LoginBean;
import com.example.week2.Moudel.utlis.OkHttpUtlis;
import com.example.week2.Presenter.inter.LoginInterPst;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by FengZhangWei on 2017/12/9.
 */

public class LoginMod {
    private LoginInterPst loginInterPst;
    public LoginMod(LoginInterPst loginInterPst) {
        this.loginInterPst=loginInterPst;
    }

    public void setdata(String s, String lophe, String lopwd) {
        Map<String, String> parmas=new HashMap<>();
        parmas.put("mobile",lophe);
        parmas.put("password",lopwd);
        OkHttpUtlis.doPost(s, parmas, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                loginInterPst.onError();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful()){
                String string = response.body().string();
                LoginBean bean = new Gson().fromJson(string, LoginBean.class);
                loginInterPst.onSuccess(bean);
            }
            }
        });
    }
}
 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
package com.example.week2.Moudel;

import com.example.week2.Moudel.bean.RegisterBean;
import com.example.week2.Moudel.utlis.OkHttpUtlis;
import com.example.week2.Presenter.inter.RegisterInterPst;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by FengZhangWei on 2017/12/9.
 */

public class RegisterMod {
    private RegisterInterPst registerPst;

    public RegisterMod(RegisterInterPst registerPst) {
        this.registerPst=registerPst;
    }

    public void regdata(String s, String phe, String pwd) {
          //解析
        Map<String, String> parmase=new HashMap<>();
        parmase.put("mobile",phe);
        parmase.put("password",pwd);
        OkHttpUtlis.doPost(s, parmase, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
              if (response.isSuccessful()){
                  String string = response.body().string();
                  RegisterBean bean = new Gson().fromJson(string, RegisterBean.class);
                  registerPst.onsuccess(bean);
              }
            }
        });
    }
}

两个布局
mainactivity的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.week2.View.MainActivity">
    <TextView
        android:text="登录"
        android:textSize="30dp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <TextView
        android:background="#AAABB3"
        android:layout_width="match_parent"
        android:layout_height="5dp" />
    <EditText
        android:id="@+id/loginphone"
        android:hint="请输入手机号"
        android:layout_gravity="center_horizontal"
        android:layout_width="300dp"
        android:background="@drawable/yuan"
        android:layout_marginTop="20dp"
        android:layout_height="50dp" />
    <EditText
        android:id="@+id/loginpassword"
        android:hint="请输入密码"
        android:layout_gravity="center_horizontal"
        android:layout_width="300dp"
        android:background="@drawable/yuan"
        android:layout_marginTop="20dp"
        android:layout_height="50dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_marginTop="10dp"
        android:layout_height="wrap_content">
        <Button
            android:onClick="Loginyes"
            android:layout_weight="1"
            android:text="登录"
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:onClick="skipRegister"
            android:layout_weight="1"
            android:text="注册"
            android:textSize="25dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</LinearLayout>
registactivity的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.example.week2.View.RegisterActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
        <TextView
            android:text="<"
            android:onClick="fanhui"
            android:textSize="45dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <TextView
            android:text="注册"
            android:textSize="30dp"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <TextView
        android:background="#AAABB3"
        android:layout_width="match_parent"
        android:layout_height="5dp" />
    <EditText
        android:id="@+id/registerphone"
        android:hint="请输入手机号"
        android:layout_gravity="center_horizontal"
        android:layout_width="300dp"
        android:background="@drawable/yuan"
        android:layout_marginTop="20dp"
        android:layout_height="50dp" />
    <EditText
        android:id="@+id/registerpassword"
        android:hint="请输入密码"
        android:layout_gravity="center_horizontal"
        android:layout_width="300dp"
        android:background="@drawable/yuan"
        android:layout_marginTop="20dp"
        android:layout_height="50dp" />

    <Button
        android:onClick="Register"
        android:text="注册"
        android:textSize="25dp"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal"
        android:layout_width="150dp"
        android:layout_height="wrap_content" />

</LinearLayout>

drawable下的 yuan.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <corners android:radius="15dp"/>
    <solid android:color="#FFFFFF" />
    <stroke android:color="#00AE00" android:width="2dp"/>
</shape>

在清单文件中再加上一个权限,请求网络的

build.gradle中

compile 'com.android.support:appcompat-v7:26.+'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.squareup.okhttp3:okhttp:3.6.0'
    compile 'com.squareup.okio:okio:1.11.0'
    compile 'com.google.code.gson:gson:2.2.4'
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:recyclerview-v7:26.+'
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.5'

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值