//布局
//写一个API
//然后MVP
// 然后 登录的Activity
package com.bwei.www.fjd.activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bwei.www.fjd.R;
import com.bwei.www.fjd.bean.LoginBean;
import com.bwei.www.fjd.presenter.LoginPresenter;
import com.bwei.www.fjd.view.LoginView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class LoginActivity extends AppCompatActivity implements LoginView {
@BindView(R.id.cha_back)
ImageView chaBack;
@BindView(R.id.ed_mobile)
EditText edMobile;
@BindView(R.id.ed_password)
EditText edPassword;
@BindView(R.id.btn_login)
Button btnLogin;
@BindView(R.id.txt_regist)
TextView txtRegist;
@BindView(R.id.dsf)
ImageView dsf;
private String mobile;
private String password;
private LoginPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
presenter = new LoginPresenter();
presenter.attach(this);
}
@Override
public void success(LoginBean loginBean) {
Toast.makeText(this, "登录成功", Toast.LENGTH_LONG).show();
SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
sp.edit().putString("username", loginBean.getData().getUsername()).putString("login", "true").putInt("uid", loginBean.getData().getUid()).commit();
finish();
}
@Override
public void failed(Throwable t) {
Toast.makeText(this, "登录失败", Toast.LENGTH_LONG).show();
}
@OnClick({R.id.cha_back, R.id.ed_mobile, R.id.ed_password, R.id.btn_login, R.id.txt_regist, R.id.dsf})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.cha_back:
finish();
break;
case R.id.ed_mobile:
break;
case R.id.ed_password:
break;
case R.id.btn_login:
mobile = edMobile.getText().toString().trim();
password = edPassword.getText().toString();
if (!TextUtils.isEmpty(mobile) && !TextUtils.isEmpty(password)) {
if (mobile.length() != 11) {
Toast.makeText(this, "请核查手机号是否正确", Toast.LENGTH_LONG).show();
} else {
presenter.login(mobile, password);
}
} else {
Toast.makeText(this, "手机号或密码不能为空", Toast.LENGTH_LONG).show();
}
break;
case R.id.txt_regist:
Intent intent = new Intent(this, RegistActivity.class);
startActivity(intent);
break;
case R.id.dsf:
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (presenter != null) {
presenter.detach();
}
}
}
//注册页面的布局
//注册页面的主页面
package com.bwei.www.fjd.activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.bwei.www.fjd.R;
import com.bwei.www.fjd.bean.RegistBean;
import com.bwei.www.fjd.presenter.RegistPresenter;
import com.bwei.www.fjd.view.RegistView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class RegistActivity extends AppCompatActivity implements RegistView{
@BindView(R.id.txt_regist_back)
TextView txtRegistBack;
@BindView(R.id.txt_qiyezhuce)
TextView txtQiyezhuce;
@BindView(R.id.txt_regist_mobile)
EditText txtRegistMobile;
@BindView(R.id.txt_regist_password)
EditText txtRegistPassword;
@BindView(R.id.btn_regist)
Button btnRegist;
@BindView(R.id.lef)
TextView lef;
@BindView(R.id.txt_lianxikefu)
TextView txtLianxikefu;
private RegistPresenter presenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_regist);
ButterKnife.bind(this);
presenter = new RegistPresenter();
presenter.attach(this);
}
@OnClick({R.id.txt_regist_back, R.id.btn_regist})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.txt_regist_back:
finish();
break;
case R.id.btn_regist:
String mobile = txtRegistMobile.getText().toString().trim();
String password = txtRegistPassword.getText().toString();
if (!TextUtils.isEmpty(mobile) && !TextUtils.isEmpty(password)) {
if (mobile.length() != 11) {
Toast.makeText(this, "请核查手机号是否正确", Toast.LENGTH_LONG).show();
} else {
presenter.regist(mobile,password);
}
} else {
Toast.makeText(this, "手机号或密码不能为空", Toast.LENGTH_LONG).show();
}
break;
}
}
@Override
public void success(RegistBean registBean) {
Toast.makeText(this, "注册成功", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
}
@Override
public void failed(Exception e) {
Toast.makeText(this, "注册失败,请重新注册" + e, Toast.LENGTH_LONG).show();
}
}
//API
//登录的MVP
//view
//model
//presenter
package com.bwei.www.fjd.presenter;
import com.bwei.www.fjd.bean.LoginBean;
import com.bwei.www.fjd.model.LoginModel;
import com.bwei.www.fjd.view.LoginView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
/**
* Created by asus on 2018/11/14.
*/
public class LoginPresenter {
private LoginModel model;
private LoginView iv;
public void attach(LoginView iv){
this.iv = iv;
model = new LoginModel();
}
public void detach(){
if (iv != null){
iv = null;
}
}
public void login(String mobile,String password){
model.login(mobile,password)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(LoginBean loginBean) throws Exception {
if (loginBean != null && "0".equals(loginBean.getCode())) {
if (iv != null) {
iv.success(loginBean);
return;
}
}
if (iv != null) {
iv.failed(new Exception("服务器未响应"));
}
}
}, new Consumer() {
@Override
public void accept(Throwable throwable) throws Exception {
if (iv != null){
iv.failed(new Exception("网络异常"));
}
}
});
}
}
//注册的MVP
//view
//model
//presenter
package com.bwei.www.fjd.presenter;
import com.bwei.www.fjd.bean.RegistBean;
import com.bwei.www.fjd.model.RegistModel;
import com.bwei.www.fjd.view.RegistView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
/**
* Created by asus on 2018/11/14.
*/
public class RegistPresenter {
private RegistModel model;
private RegistView iv;
public void attach(RegistView iv){
this.iv = iv;
model = new RegistModel();
}
public void regist(String mobile,String password){
model.regist(mobile, password)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(RegistBean registBean) throws Exception {
if (registBean != null && "1".equals(registBean.getCode())) {
if (iv != null) {
iv.success(registBean);
}
return;
}
if (iv != null) {
iv.failed(new Exception("服务器未响应"));
}
}
}, new Consumer() {
@Override
public void accept(Throwable throwable) throws Exception {
if (iv != null){
iv.failed(new Exception("网络请求失败"));
}
}
});
}
}
//退出登录的代码
package com.bwei.www.fjd.activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bwei.www.fjd.R;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
public class CloseActivity extends AppCompatActivity {
@BindView(R.id.img_return)
ImageView imgReturn;
@BindView(R.id.txt_service)
TextView txtService;
@BindView(R.id.img_head)
ImageView imgHead;
@BindView(R.id.txt_name)
TextView txtName;
@BindView(R.id.ll_login)
LinearLayout llLogin;
@BindView(R.id.btn_logout)
Button btnLogout;
@BindView(R.id.address)
TextView address;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_close);
ButterKnife.bind(this);
SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
String username = sp.getString("username", null);
txtName.setText(username);
}
@OnClick({R.id.img_return, R.id.btn_logout})
public void onViewClicked(View view) {
switch (view.getId()) {
case R.id.img_return:
finish();
break;
case R.id.btn_logout:
SharedPreferences sp = getSharedPreferences("config", MODE_PRIVATE);
sp.edit().putString("login", "false").commit();
finish();
break;
}
}
}
我的页面Fragment
package com.bwei.www.fjd.fragment; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import android.widget.Toast; import com.bwei.www.fjd.R; import com.bwei.www.fjd.activity.CloseActivity; import com.bwei.www.fjd.activity.LoginActivity; import com.bwei.www.fjd.activity.WebViewActivity; import com.bwei.www.fjd.adapter.TuiAdapter; import com.bwei.www.fjd.bean.TuiBean; import com.bwei.www.fjd.presenter.TuiPresenter; import com.bwei.www.fjd.view.ITuiView; import com.facebook.drawee.view.SimpleDraweeView; import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; import butterknife.Unbinder; /** * Created by asus on 2018/11/7. */ public class MineFragment extends Fragment implements ITuiView { @BindView(R.id.img_head) SimpleDraweeView imgHead; @BindView(R.id.txt_head) TextView txtHead; Unbinder unbinder; @BindView(R.id.tui) RecyclerView tui; private Listlist; private TuiAdapter adapter; private TuiPresenter tuiPresenter; private SharedPreferences sp; private String login; private String username; private Intent intent; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View v = inflater.inflate(R.layout.item_mine, container, false); unbinder = ButterKnife.bind(this, v); return v; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); tuiPresenter = new TuiPresenter(); tuiPresenter.attach(this); GridLayoutManager manager = new GridLayoutManager(getActivity(),2); tui.setLayoutManager(manager); tuiPresenter.getTui(); list = new ArrayList<>(); adapter = new TuiAdapter(list,getContext()); tui.setAdapter(adapter); //点击条目跳转详情页面 adapter.setOnItemClickListener(new TuiAdapter.OnItemClickListener() { @Override public void OnClick(int position, View view) { TuiBean.DataBean.TuijianBean.ListBeanX listBeanX = list.get(position); Intent intent = new Intent(getActivity(), WebViewActivity.class); //传对应的url和pid intent.putExtra("url", listBeanX.getDetailUrl()); //intent.putExtra("pid", listBeanX.getPid()); startActivity(intent); } }); } @Override public void onResume() { super.onResume(); sp = getActivity().getSharedPreferences("config", Context.MODE_PRIVATE); username = sp.getString("username", null); login = sp.getString("login", ""); if (login.equals("true")) { txtHead.setText(username); } else { txtHead.setText("登录/注册>"); } } // @OnClick(R.id.txt_head) // public void onViewClicked(View view) { // switch (view.getId()){ // case R.id.txt_head: // if (login.equals("true")){ // intent = new Intent(getContext(), CloseActivity.class); // startActivity(intent); // }else { // intent = new Intent(getContext(), LoginActivity.class); // startActivity(intent); // } // // break; // } // } @OnClick({R.id.img_head, R.id.txt_head}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.img_head: Intent intent = new Intent(getContext(), LoginActivity.class); startActivity(intent); break; case R.id.txt_head: Intent intent2 = new Intent(getContext(), CloseActivity.class); startActivity(intent2); break; } } @Override public void getTui(TuiBean tuiBean) { list.clear(); list.addAll(tuiBean.getData().getTuijian().getList()); adapter.notifyDataSetChanged(); } @Override public void failed(Throwable t) { Toast.makeText(getContext(),""+t.getMessage(),Toast.LENGTH_LONG).show(); } @Override public void onDestroyView() { super.onDestroyView(); unbinder.unbind(); if (tuiPresenter!= null){ tuiPresenter.detach(); } } }