登录布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/login_title_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:id="@+id/finish"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@mipmap/cuowu" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="京东登录" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/login_title_relative"
android:layout_margin="10dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号" />
<EditText
android:id="@+id/edit_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入手机号" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码" />
<EditText
android:id="@+id/edit_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword"
android:singleLine="true" />
</LinearLayout>
<Button
android:id="@+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="登录" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<TextView
android:id="@+id/text_regist"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="手机快速注册" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码" />
</LinearLayout>
</LinearLayout>
-
<!--<LinearLayout-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_alignParentBottom="true"-->
<!--android:layout_marginBottom="20dp"-->
<!--android:orientation="horizontal">-->
<!--<ImageView-->
<!--android:id="@+id/login_by_wechat"-->
<!--android:layout_width="0dp"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_weight="1"-->
<!--android:src="@drawable/weixin" />-->
<!--<ImageView-->
<!--android:id="@+id/login_by_qq"-->
<!--android:layout_width="0dp"-->
<!--android:layout_height="wrap_content"-->
<!--android:layout_weight="1"-->
<!--android:src="@drawable/mqq" />-->
<!--</LinearLayout>-->
</RelativeLayout>
activity
public class LoginActivity extends BaseActivity<LoginPresenter> implements ILoginView {
@BindView(R.id.finish)
ImageView finish;
@BindView(R.id.login_title_relative)
RelativeLayout loginTitleRelative;
@BindView(R.id.edit_phone)
EditText editPhone;
@BindView(R.id.edit_pwd)
EditText editPwd;
@BindView(R.id.btnLogin)
Button btnLogin;
@BindView(R.id.text_regist)
TextView textRegist;
@Override
protected LoginPresenter ProvidePresenter() {
presenter = new LoginPresenter(this);
return presenter;
}
//设置监听
@Override
protected void initListener() {
}
//初始化数据
@Override
protected void initData() {
}
//设置布局ID
@Override
protected int provideLayoutId() {
return R.layout.activity_login;
}
@Override
public Context context() {
return this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO: add setContentView(...) invocation
ButterKnife.bind(this);
EventBus.getDefault().register(this);
}
@OnClick({R.id.finish, R.id.btnLogin, R.id.text_regist})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.finish:
finish();
break;
case R.id.btnLogin:
String mobile = editPhone.getText().toString();
String password = editPwd.getText().toString();
presenter.getLogin(mobile,password);
break;
case R.id.text_regist:
Intent intent = new Intent(this, RegisterActivity.class);
startActivity(intent);
break;
}
}
@Override
public void Show(LoginBean loginBean) {
int uid = loginBean.getData().getUid();
String username = loginBean.getData().getUsername();
String token = loginBean.getData().getToken();
SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("name",username);
editor.putInt("uid",uid);
editor.putString("token",token);
editor.commit();
if (loginBean.getCode().equals("0")){
Intent intent = new Intent();
intent.putExtra("uid",uid+"");
intent.putExtra("token",token);
setResult(2,intent);
finish();
}
Toast.makeText(this,loginBean.getMsg(),Toast.LENGTH_LONG).show();
}
@Override
public void ShowRegister(RegisterBean registerBean) {
}
@Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
public void Event(RegistInfoBean registInfoBean){
String mobile = registInfoBean.getMobile();
String password = registInfoBean.getPassword();
editPhone.setText(mobile);
editPwd.setText(password);
}
}
注册布局
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/login_title_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:id="@+id/finish"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@mipmap/return_img" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="京东注册" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/login_title_relative"
android:layout_margin="10dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号" />
<EditText
android:id="@+id/reg_edit_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入手机号" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码" />
<EditText
android:id="@+id/reg_edit_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword"
android:singleLine="true" />
</LinearLayout>
<Button
android:id="@+id/btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="注册" />
</LinearLayout>
</RelativeLayout>
activity
public class RegisterActivity extends BaseActivity<LoginPresenter> implements ILoginView { @BindView(R.id.finish) ImageView finish; @BindView(R.id.login_title_relative) RelativeLayout loginTitleRelative; @BindView(R.id.reg_edit_phone) EditText regEditPhone; @BindView(R.id.reg_edit_pwd) EditText regEditPwd; @BindView(R.id.btn_register) Button btnRegister; private String mobile; private String password; @Override public Context context() { return this; } @Override public void Show(LoginBean loginBean) { } @Override public void ShowRegister(RegisterBean registerBean) { String msg = registerBean.getMsg(); String code = registerBean.getCode(); if (code.equals("0")&&msg.equals("注册成功")){ EventBus.getDefault().postSticky(new RegistInfoBean(mobile, password)); finish(); }else if(code.equals("1")&&msg.equals("天呢!用户已注册")) { finish(); } Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); } @Override protected LoginPresenter ProvidePresenter() { presenter = new LoginPresenter(this); return presenter; } @Override protected void initListener() { } @Override protected void initData() { } @Override protected int provideLayoutId() { return R.layout.activity_regist; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // TODO: add setContentView(...) invocation ButterKnife.bind(this); } @OnClick({R.id.finish, R.id.btn_register}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.finish: finish(); break; case R.id.btn_register: mobile = regEditPhone.getText().toString().trim(); password = regEditPwd.getText().toString().trim(); presenter.getReg(mobile, password); break; } } }