注册的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:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:id="@+id/userName"
android:padding="16dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:id="@+id/pass"
android:padding="16dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="@string/app_reg"
android:id="@+id/reg"
android:layout_gravity="center"
/>
</LinearLayout>
主代码
package bwie.com.app1;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import bwie.com.app1.base.BaseActivity;
import bwie.com.app1.present.RegInterPresenter;
import bwie.com.app1.present.RegPresentClass;
public class RegActivity extends BaseActivity implements View.OnClickListener {
private EditText userName;
private EditText pass;
private RegInterPresenter regInterPresenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
protected void initData() {
}
@Override
protected void initView() {
userName = findViewById(R.id.userName);
pass = findViewById(R.id.pass);
findViewById(R.id.reg).setOnClickListener(this);
//初始化presenter
regInterPresenter = new RegPresentClass(this);
}
@Override
protected int initLayout() {
return R.layout.activity_reg;
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.reg:
regInterPresenter.getRegPresentData(userName.getText().toString(),pass.getText().toString());
Intent intent = new Intent(RegActivity.this, MainActivity.class);
startActivity(intent);
break;
}
}
}
注册的model 接口
package bwie.com.app1.model;
public interface InterReg
{
void getRegData(String url,String name,String pwd,RegBack regBack);
//注册成功失败回调方法
interface RegBack{
void LoadSuccess(String data);
void LoadError();
}
}
注册的model.class
package bwie.com.app1.model;
import android.util.Log;
import bwie.com.app1.api.Api;
import bwie.com.app1.network.MyTask;
import bwie.com.app1.network.OkHttp3;
public class RegClass implements InterReg
{
@Override
public void getRegData(final String url, final String name, final String pwd, final RegBack regBack) {
new Runnable(){
@Override
public void run() {
new OkHttp3().okHttpPost(url,name,pwd);
}
}.run();
}
}
注册的presenter接口
package bwie.com.app1.present;
public interface RegInterPresenter
{
void getRegPresentData(String name,String pwd);
}
注册的presenter.class
package bwie.com.app1.present;
import android.content.Intent;
import android.util.Log;
import bwie.com.app1.MainActivity;
import bwie.com.app1.RegActivity;
import bwie.com.app1.api.Api;
import bwie.com.app1.model.InterReg;
import bwie.com.app1.model.RegClass;
public class RegPresentClass implements RegInterPresenter
{
private final RegClass regClass;
RegActivity regActivity;
public RegPresentClass(RegActivity regActivity) {
//初始化model
regClass = new RegClass();
this.regActivity=regActivity;
}
@Override
public void getRegPresentData(String name, String pwd) {
regClass.getRegData(Api.REG, name, pwd, new InterReg.RegBack() {
@Override
public void LoadSuccess(String data) {
Log.i("LoadSuccess",data+"");
}
@Override
public void LoadError() {
Log.i("LoadError","失败了");
}
});
}
}
注册成功之后跳到登录页面