仿京東——我的介面(登入註冊,上傳頭像)
阿新 • • 發佈:2018-12-11
我的介面 佈局
<?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" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:id="@+id/my_head" android:layout_width="match_parent" android:layout_height="170dp" android:background="@drawable/loginbefore"> <com.facebook.drawee.view.SimpleDraweeView android:id="@+id/my_icon" android:layout_width="70dp" android:layout_height="70dp" android:layout_alignParentStart="true" android:layout_centerVertical="true" android:layout_marginStart="21dp" app:placeholderImage="@drawable/loginbefore" app:roundAsCircle="true" /> <TextView android:id="@+id/my_name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentStart="true" android:layout_centerVertical="true" android:layout_marginStart="104dp" android:text="登入/註冊>" android:textColor="@color/white" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/my_order" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="我的訂單" /> </LinearLayout> </LinearLayout>
我的介面 程式碼
public class MyFragment extends Fragment { @BindView(R.id.my_icon) SimpleDraweeView myIcon; @BindView(R.id.my_name) TextView myName; @BindView(R.id.my_head) RelativeLayout myHead; Unbinder unbinder; @BindView(R.id.my_order) Button myOrder; private SharedPreferences preferences; @Nullable @Override public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = View.inflate(getActivity(), R.layout.my_fg, null); unbinder = ButterKnife.bind(this, view); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } private int getIsLogin() { // SharedPreferences preferences = getActivity().getSharedPreferences("user", 0); return preferences.getInt("islogin", 0); } @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); } @Override public void onResume() { super.onResume(); preferences = getActivity().getSharedPreferences("user", 0); if (getIsLogin() == 1) { String icon = preferences.getString("icon", "null"); String nickname = preferences.getString("nickname", "null"); myHead.setBackground(getResources().getDrawable(R.drawable.loginafter)); myIcon.setImageURI(Uri.parse(icon)); myName.setText(nickname); //***************************************** myHead.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(getActivity(), UserSetActivity.class)); } }); } else { myHead.setBackground(getResources().getDrawable(R.drawable.loginbefore)); //設定本地圖片 myIcon.setActualImageResource(R.drawable.user); myName.setText("登入/註冊>"); myHead.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(getActivity(), LoginActivity.class)); } }); } } @OnClick(R.id.my_order) public void onViewClicked() { startActivity(new Intent(getActivity(), MyOrderActivity.class)); } }
mvp-moedl-RegisterModel
public class RegisterModel {
public Observable<Register> Register(String name, String pwd) {
return RetrofitUtil.getDefault().create(MyRetrofit.class).getRegister(name, pwd);
}
}
mvp-model-LoginModel
public class LoginModel { public Observable<Login> login(String name, String pwd) { return RetrofitUtil.getDefault().create(MyRetrofit.class).getLogin(name, pwd); } }
mvp-presenter-RegisterPresenter
public class RegisterPresenter extends BasePresenter<RegisterView> {
private RegisterModel registerModel;
public RegisterPresenter(RegisterView view) {
super(view);
}
@Override
public void initModel() {
registerModel = new RegisterModel();
}
public void register(String name, String pwd){
registerModel.Register(name,pwd)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Register>() {
@Override
public void onSubscribe(Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onNext(Register register) {
view.onRegisterSuccess(register);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}
mvp-model-LoginPresenter
public class LoginPresenter extends BasePresenter<LoginView> {
private LoginModel loginModel;
public LoginPresenter(LoginView view) {
super(view);
}
@Override
public void initModel() {
loginModel = new LoginModel();
}
public void login(String name, String pwd){
loginModel.login(name,pwd)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Login>() {
@Override
public void onSubscribe(Disposable d) {
compositeDisposable.add(d);
}
@Override
public void onNext(Login login) {
view.onLoginSuccess(login);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
}
mvp-model-registerView
public interface RegisterView extends IView {
void onRegisterSuccess(Register register);
}
mvp-model-loginView
public interface LoginView extends IView {
void onLoginSuccess(Login login);
}
跳轉到LoginActivity
public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginView, View.OnClickListener {
@BindView(R.id.login_name)
EditText loginName;
@BindView(R.id.login_pwd)
EditText loginPwd;
@BindView(R.id.login_login)
Button loginLogin;
@BindView(R.id.login_reg)
TextView loginReg;
private String name;
private String pwd;
@Override
public LoginPresenter providePresenter() {
return new LoginPresenter(this);
}
@Override
public int provideLayoutId() {
return R.layout.activity_login;
}
@Override
public void initListener() {
loginLogin.setOnClickListener(this);
loginReg.setOnClickListener(this);
}
@Override
public void initDate() {
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login_login:
name = loginName.getText().toString().trim();
pwd = loginPwd.getText().toString().trim();
presenter.login(name, pwd);
break;
case R.id.login_reg:
startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
break;
}
}
@Override
public void onLoginSuccess(Login login) {
String code = login.getCode();
if (code.equals("0")) {
Login.DataBean data = login.getData();
//登入成功
Toast.makeText(LoginActivity.this, "登入成功", Toast.LENGTH_SHORT).show();
//將是否登入狀態值和uid存入sp中
SharedPreferences.Editor editor = getSharedPreferences("user", 0).edit();
editor.putInt("islogin",1);
editor.putString("name",name);
editor.putString("pwd",pwd);
editor.putInt("uid",data.getUid());
editor.putString("icon",data.getIcon());
editor.putString("nickname",data.getNickname());
editor.commit();
finish();
} else {
Toast.makeText(LoginActivity.this, "使用者名稱或密碼錯誤!", Toast.LENGTH_SHORT).show();
}
}
}
LoginActivity佈局
<?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=".mvp.view.activity.LoginActivity">
<EditText
android:id="@+id/login_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入手機號" />
<EditText
android:id="@+id/login_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入密碼" />
<Button
android:id="@+id/login_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登入" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/login_reg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:text="新使用者註冊" />
</RelativeLayout>
</LinearLayout>
RegisterActivity
public class RegisterActivity extends BaseActivity<RegisterPresenter> implements RegisterView {
@BindView(R.id.reg_name)
EditText regName;
@BindView(R.id.reg_pwd)
EditText regPwd;
@BindView(R.id.reg_reg)
Button regReg;
@Override
public RegisterPresenter providePresenter() {
return new RegisterPresenter(this);
}
@Override
public int provideLayoutId() {
return R.layout.activity_register;
}
@Override
public void initListener() {
}
@Override
public void initDate() {
openDialog();
}
@Override
public void onRegisterSuccess(Register register) {
String code = register.getCode();
if (code.equals("0")) {
Toast.makeText(RegisterActivity.this, "註冊成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(RegisterActivity.this, register.getMsg(), Toast.LENGTH_SHORT).show();
}
}
@OnClick(R.id.reg_reg)
public void onViewClicked() {
String name = regName.getText().toString().trim();
String pwd = regPwd.getText().toString().trim();
presenter.register(name, pwd);
}
private void openDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("註冊協議及隱私政策");
builder.setMessage("給我同意");
builder.setPositiveButton("同意", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setNegativeButton("不同意", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
AlertDialog alertDialog = builder.create();
//設定點選返回按鈕不能關閉AlertDialog
alertDialog.setCancelable(false);
//設定點選alertDialog外部區域不能關閉AlertDialog
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.show();
}
}
RegisterActivity佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
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"
tools:context=".mvp.view.activity.RegisterActivity">
<EditText
android:id="@+id/reg_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入手機號" />
<EditText
android:id="@+id/reg_pwd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入密碼" />
<Button
android:id="@+id/reg_reg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="註冊" />
</LinearLayout>