首先使用的是MVP框架实现,
注册的布局
<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="com.example.desktop.monthshoppingcart.view.activity.RegisterActivity"> <RelativeLayout android:layout_width="match_parent" android:background="#f98e2b" android:layout_height="60dp"> <TextView android:id="@+id/txtReturn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18dp" android:text="X" android:layout_centerVertical="true" android:layout_marginLeft="7dp" android:textColor="#fff" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="18dp" android:textColor="#fff" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="新用户注册" /> </RelativeLayout> <EditText android:id="@+id/reg_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="30dp" android:hint="请输入用户名"/> <EditText android:id="@+id/reg_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="请输入密码"/> <EditText android:id="@+id/con_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="确认输入密码"/> <EditText android:id="@+id/reg_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="请填写邮箱"/> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/button_shape" android:layout_marginTop="20dp" android:onClick="reg_btn" android:text="注册" /> </LinearLayout>
注册的activity:
public class RegisterActivity extends AppCompatActivity implements Register_face{ @BindView(R.id.txtReturn) TextView txtReturn; @BindView(R.id.reg_name) EditText regName; @BindView(R.id.reg_pwd) EditText regPwd; @BindView(R.id.con_pwd) EditText conPwd; @BindView(R.id.reg_email) EditText regEmail; private RegisterPresenter registerPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); ButterKnife.bind(this); registerPresenter = new RegisterPresenter(this); } public void reg_btn(View view) { String regname = regName.getText().toString(); String regpwd = regPwd.getText().toString(); registerPresenter.setData(ApiUrl.reg_url,regname,regpwd); } @Override public void setVSuccess(RegisterBean registerBean) { if ("0".equals(registerBean.getCode())){ Toast.makeText(RegisterActivity.this,registerBean.getMsg(),Toast.LENGTH_SHORT).show(); finish(); }else { Toast.makeText(RegisterActivity.this,registerBean.getMsg(),Toast.LENGTH_SHORT).show(); } } @Override public void setVError(String s) { } }
登录布局:
<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" android:layout_marginLeft="10dp" tools:context="com.example.desktop.monthshoppingcart.view.activity.LoginActivity"> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:layout_marginTop="30dp" android:layout_marginBottom="30dp" android:layout_gravity="center" android:src="@drawable/tb" /> <EditText android:id="@+id/log_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="手机号/会员名/邮箱"/> <EditText android:id="@+id/log_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="请输入密码"/> <RelativeLayout android:layout_width="match_parent" android:layout_height="60dp" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:text="忘记密码"/> <TextView android:id="@+id/register" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="10dp" android:text="新用户注册"/> </RelativeLayout> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/button_shape" android:onClick="log_btn" android:text="登录" /> <ImageView android:layout_width="50dp" android:layout_height="50dp" android:layout_gravity="center_horizontal" android:layout_marginTop="15dp" android:src="@drawable/qq"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="QQ登录"/> </LinearLayout>
登录的activity:
public class LoginActivity extends AppCompatActivity implements Login_face{ @BindView(R.id.log_name) EditText logName; @BindView(R.id.log_pwd) EditText logPwd; @BindView(R.id.register) TextView register; private LoginPresenter loginPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); ButterKnife.bind(this); loginPresenter = new LoginPresenter(this ); register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent=new Intent(LoginActivity.this,RegisterActivity.class); startActivity(intent); } }); } public void log_btn(View view) { String logname = logName.getText().toString(); String logpwd = logPwd.getText().toString(); loginPresenter.setData(ApiUrl.login_url,logname,logpwd); } @Override public void setVSuccess(LoginBean loginBean) { Log.d("LoginActivity-------",loginBean.toString()); if ("0".equals(loginBean.getCode())){ Toast.makeText(LoginActivity.this,loginBean.getMsg(),Toast.LENGTH_SHORT).show(); Intent intent=new Intent(LoginActivity.this,ListActivity.class); startActivity(intent); }else { Toast.makeText(LoginActivity.this, loginBean.getMsg(),Toast.LENGTH_SHORT).show(); } } @Override public void setVError(String s) { } }
public class RegisterModel { private Register_inter register_inter; public RegisterModel(Register_inter register_inter) { this.register_inter=register_inter; } public void setUserData(String reg_url, String regname, String regpwd) { Map<String, String> map=new HashMap<>(); map.put("mobile",regname); map.put("password",regpwd); RetrofitUtil.getApiService(reg_url).reg_url(map) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new Subscriber<String>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { } @Override public void onNext(String s) { Log.d("RegModel-------------",s); if (s!=null){ RegisterBean registerBean = new Gson().fromJson(s, RegisterBean.class); register_inter.setSuccess(registerBean); } } }); } }