主activity.xml布局
<?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"> <EditText android:id="@+id/userName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" android:layout_marginTop="10dp" android:padding="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" /> <EditText android:id="@+id/pass" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:layout_marginTop="10dp" android:padding="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/logins" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="登录" android:layout_marginTop="50dp" android:layout_marginLeft="10dp" /> <Button android:id="@+id/res" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:layout_marginTop="50dp" android:layout_marginLeft="50dp" /> </LinearLayout> </LinearLayout>
注册布局
<?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=".ZhuCeActivity"> <EditText android:id="@+id/resName" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名" android:layout_marginTop="10dp" android:padding="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" /> <EditText android:id="@+id/resPass" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:layout_marginTop="10dp" android:padding="10dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <Button android:id="@+id/zhuce" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="注册" android:layout_marginTop="50dp" android:layout_marginLeft="50dp" /> </LinearLayout> </LinearLayout>
登录presenter
package bwie.com.lenovo.day18.presenter; import bwie.com.lenovo.day18.MainActivity; import bwie.com.lenovo.day18.model.LoginModel; import bwie.com.lenovo.day18.view.ILoginView; public class LoginPresenter { private final LoginModel loginModel; ILoginView iLoginView; public LoginPresenter(ILoginView loginView) { loginModel = new LoginModel(); iLoginView = loginView; } public void getDataPresenter(String name, String pass){ loginModel.showDataModel(name,pass); loginModel.setOnLoginListener(new LoginModel.OnLoginListener() { @Override public void result(String status) { iLoginView.getData(status); } }); } }
// 注册 presenter
package bwie.com.lenovo.day18.presenter; import bwie.com.lenovo.day18.model.ZhuCeModel; import bwie.com.lenovo.day18.view.IZhuCeView; public class ZhuCePresenter { private IZhuCeView isZhuCeView; private final ZhuCeModel zhuCeModel; public ZhuCePresenter(IZhuCeView iZhuCeView) { isZhuCeView = iZhuCeView; zhuCeModel = new ZhuCeModel(); } public void zhuCePreData(String name, String pass){ zhuCeModel.zhuCeModelData(name,pass); zhuCeModel.setiZhuceDa(new ZhuCeModel.IZhuceDa() { @Override public void stulr(String status) { isZhuCeView.getDataZhuCe(status); } }); } }
//登录model
package bwie.com.lenovo.day18.model; import android.os.Handler; import android.os.Message; import android.util.Log; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class LoginModel { //创建接口 public interface OnLoginListener{ void result(String status); } private OnLoginListener onLoginListener; public void setOnLoginListener(OnLoginListener onLoginListener) { this.onLoginListener = onLoginListener; } private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case 0: String json = (String) msg.obj; Log.i("aaa",json); try { JSONObject jsonObject = new JSONObject(json); String status = jsonObject.getString("status"); if (onLoginListener!=null){ onLoginListener.result(status); } } catch (Exception e) { e.printStackTrace(); } break; } } }; public void showDataModel(String name,String pass){ OkHttpClient okHttpClient = new OkHttpClient(); RequestBody requestBody = new FormBody.Builder() .add("phone",name) .add("pwd",pass) .build(); Request request = new Request.Builder() .url("http://172.17.8.100/small/user/v1/login") .post(requestBody) .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 { String string = response.body().string(); Message message = new Message(); message.what = 0; message.obj = string; handler.sendMessage(message); } }); } }
//注册model
package bwie.com.lenovo.day18.model; import android.os.Handler; import android.os.Message; import android.util.Log; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; public class ZhuCeModel { public interface IZhuceDa{ void stulr(String status); } private IZhuceDa iZhuceDa; public void setiZhuceDa(IZhuceDa iZhuceDa) { this.iZhuceDa = iZhuceDa; } private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); switch (msg.what){ case 0: String json = (String) msg.obj; Log.i("bbbb",json); try { JSONObject jsonObject = new JSONObject(json); String status = jsonObject.getString("status"); if (iZhuceDa!=null){ iZhuceDa.stulr(status); } } catch (Exception e) { e.printStackTrace(); } break; } } }; public void zhuCeModelData(String name,String pass){ OkHttpClient okHttpClient = new OkHttpClient(); RequestBody requestBody = new FormBody.Builder() .add("phone",name) .add("pwd",pass) .build(); Request request = new Request.Builder() .url("http://172.17.8.100/small/user/v1/register") .post(requestBody) .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 { String string = response.body().string(); Message message = new Message(); message.what=0; message.obj = string; handler.sendMessage(message); } }); } }
登录主页面
package bwie.com.lenovo.day18; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import bwie.com.lenovo.day18.presenter.LoginPresenter; import bwie.com.lenovo.day18.view.ILoginView; public class MainActivity extends AppCompatActivity implements View.OnClickListener,ILoginView { private Button login; private Button res; private EditText name; private EditText pass; private LoginPresenter loginPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name = findViewById(R.id.userName); pass = findViewById(R.id.pass); login = findViewById(R.id.logins); res = findViewById(R.id.res); login.setOnClickListener(this); res.setOnClickListener(this); loginPresenter = new LoginPresenter(this); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.logins: String names = name.getText().toString(); String passs = pass.getText().toString(); if (names.length()>=11 && passs.length()>=3){ loginPresenter.getDataPresenter(names,passs); }else { Toast.makeText(this,"手机格式不对",Toast.LENGTH_SHORT).show(); } break; case R.id.res: startActivity(new Intent(MainActivity.this,ZhuCeActivity.class)); finish(); break; } } @Override public void getData(String data) { int i = Integer.parseInt(data); if (i == 0000){ Toast.makeText(this,"登录成功",Toast.LENGTH_SHORT).show(); startActivity(new Intent(MainActivity.this,ShowActivity.class)); finish(); }else { Toast.makeText(this,"登录失败",Toast.LENGTH_SHORT).show(); } } }
//注册主页面
package bwie.com.lenovo.day18; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import bwie.com.lenovo.day18.presenter.ZhuCePresenter; import bwie.com.lenovo.day18.view.ILoginView; import bwie.com.lenovo.day18.view.IZhuCeView; public class ZhuCeActivity extends AppCompatActivity implements IZhuCeView { private Button zhuce; private EditText rName; private EditText rPass; private ZhuCePresenter zhuCePresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_zhu_ce); rName = findViewById(R.id.resName); rPass = findViewById(R.id.resPass); zhuce = findViewById(R.id.zhuce); zhuCePresenter = new ZhuCePresenter(this); zhuce.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String rname = rName.getText().toString(); String rpass = rPass.getText().toString(); if (rname.length()>=11 && rpass.length()>=3){ zhuCePresenter.zhuCePreData(rname,rpass); }else { Toast.makeText(ZhuCeActivity.this,"手机格式不正确",Toast.LENGTH_SHORT).show(); } } }); } @Override public void getDataZhuCe(String data) { int i = Integer.parseInt(data); if (i == 0000){ Toast.makeText(this,"注册成功",Toast.LENGTH_SHORT).show(); startActivity(new Intent(ZhuCeActivity.this,MainActivity.class)); }else { Toast.makeText(this,"此号码已被注册",Toast.LENGTH_SHORT).show(); } } }