**## 登录
package com.bawei.article520.tab;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bawei.article520.MainActivity;
import com.bawei.article520.R;
import com.bawei.article520.ShowActivity;
import com.bawei.article520.api.Api;
import com.bawei.article520.bean.LoginBean;
import com.bawei.article520.iview.Iview;
import com.bawei.article520.presenter.Presenter;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
public class Tablogin extends Fragment implements Iview,View.OnClickListener {
private EditText editText_login_phone;
private EditText editText_login_pwd;
private CheckBox checkBox;
private Button button_login;
private TextView textView_register;
private Presenter presenter;
private ImageView image_eye;
private SharedPreferences preferences;
private SharedPreferences.Editor edit;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.tablogin_item,container,false);
editText_login_phone = view.findViewById(R.id.edit_login_phone);
editText_login_pwd = view.findViewById(R.id.edit_login_pwd);
checkBox = view.findViewById(R.id.check);
button_login = view.findViewById(R.id.btn_login);
textView_register = view.findViewById(R.id.text_register);
image_eye = view.findViewById(R.id.image_eye);
//记住密码
preferences = getActivity().getSharedPreferences("wenjian", Context.MODE_PRIVATE);
edit = preferences.edit();
boolean key = preferences.getBoolean("Key", false);
if (!key) {
editText_login_phone.setText( preferences.getString( "phone",null ) );
editText_login_pwd.setText( preferences.getString( "pwd",null ) );
checkBox.setChecked( true );;
}else{
checkBox.setChecked(false);
}
//初始化p层
presenter = new Presenter();
presenter.attchView(this);
//presenter.getData(Api.login_url,Mathis);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
image_eye.setOnClickListener(this);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (checkBox.isChecked()){
String phone = editText_login_phone.getText().toString();
String pwd = editText_login_pwd.getText().toString();
edit.putString("phone",phone);
edit.putString("pwd",pwd);
edit.putBoolean("key",true);
edit.commit();
}else if (!checkBox.isChecked()){
edit.clear();
edit.commit();
}
}
});
button_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//获取值
String phone = editText_login_phone.getText().toString();
String pwd = editText_login_pwd.getText().toString();
//判断是剖登录
if (!phone.isEmpty()&&!pwd.isEmpty()){
presenter.getData(phone,pwd,getActivity());
}
}
});
//注册
textView_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity)getActivity()).toRegist();
}
});
}
@Override
public void getData(String data) {
Gson gson=new Gson();
LoginBean loginBean = gson.fromJson(data, LoginBean.class);
Intent intent = new Intent(getActivity(), ShowActivity.class);
startActivity(intent);
if (loginBean.getStatus().equals("0000")){
Toast.makeText(getActivity(),"登入成功",Toast.LENGTH_LONG).show();
}else
{
Toast.makeText(getActivity(),"登录失败",Toast.LENGTH_LONG).show();
}
}
@Override
public void onDestroy() {
super.onDestroy();
presenter.datechView();
}
@Override
//展示 密码数据
public void onClick(View v) {
switch (v.getId()){
case R.id.image_eye://图片眼睛点击
if(editText_login_pwd.getInputType() == 128){//如果现在是显示密码模式
editText_login_pwd.setInputType(129);//设置为隐藏密码
Glide.with( this ).load( R.drawable.login_icon_phone_n_hdpi ).into( image_eye );
}else {
editText_login_pwd.setInputType(128);//设置为显示密码
Glide.with( this ).load( R.drawable.login_icon_eye_n_hdhpi ).into( image_eye );
}
break;
}
}
}
```**
## 注册
package com.bawei.article520.tab;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import com.bawei.article520.MainActivity;
import com.bawei.article520.R;
import com.bawei.article520.ShowActivity;
import com.bawei.article520.bean.RegisterBean;
import com.bawei.article520.iview.Iview;
import com.bawei.article520.presenter.Presenter;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
public class Tabregister extends Fragment implements Iview {
private EditText edit_register_phone;
private EditText edit_register_pwd;
private EditText edit_register_mode;
private Button button_register;
private Presenter presenter;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.tabregister_item,container,false);
edit_register_phone = view.findViewById(R.id.edit_register_phone);
edit_register_pwd=view.findViewById(R.id.edit_register_pwd);
edit_register_mode = view.findViewById(R.id.edit_register_mode);
button_register = view.findViewById(R.id.btn_register);
//初始化p
presenter = new Presenter();
presenter.zcattchView(this);
return view;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
button_register.setOnClickListener(new View.OnClickListener() {
private String zc_pwd;
private String zc_phone;
@Override
public void onClick(View v) {
zc_phone = edit_register_phone.getText().toString();
zc_pwd = edit_register_pwd.getText().toString();
//判断
if (!zc_phone.isEmpty()&&!zc_pwd.isEmpty()){
presenter.getData(zc_phone,zc_pwd,getActivity());
}
}
});
}
@Override
public void getData(String data) {
Gson gson=new Gson();
RegisterBean registerBean = gson.fromJson(data, RegisterBean.class);
if (registerBean.getStatus().equals("1001")){
/* Intent intent = new Intent(getActivity(), ShowActivity.class);
startActivity(intent);*/
Toast.makeText(getActivity(),"注册成功",Toast.LENGTH_LONG).show();
}else {
Toast.makeText(getActivity(),"注册失败,已有账号注册",Toast.LENGTH_LONG).show();
}
}
@Override
public void onDestroy() {
super.onDestroy();
presenter.zcdatechView();
}
}