package com.example.com.lixiaolong_04_27_.mvp.fragment1.view;
import android.content.Context;
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.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.example.com.lixiaolong_04_27_.R;
import com.example.com.lixiaolong_04_27_.mvp.fragment1.bean.UserBean;
import com.example.com.lixiaolong_04_27_.mvp.fragment1.presenter.LoginPresenter;
public class LoginActivity extends AppCompatActivity implements LoginView, View.OnClickListener {
private RelativeLayout mLoginTitleRelative;
private EditText mEditPhone;
private EditText mEditPwd;
private TextView mTextRegist;
private ImageView mLoginByWechat;
private ImageView mLoginByQq;
private LoginPresenter loginPresenter;
private Button mBtn1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
initView();
//绑定
loginPresenter=new LoginPresenter(this);
}
private void initView() {
mLoginTitleRelative = (RelativeLayout) findViewById(R.id.login_title_relative);
mEditPhone = (EditText) findViewById(R.id.edit_phone);
mEditPwd = (EditText) findViewById(R.id.edit_pwd);
mBtn1 = (Button) findViewById(R.id.btn1);
mBtn1.setOnClickListener(this);
mTextRegist = (TextView) findViewById(R.id.text_regist);
mLoginByWechat = (ImageView) findViewById(R.id.login_by_wechat);
mLoginByQq = (ImageView) findViewById(R.id.login_by_qq);
}
public void onClick(View v) {
switch (v.getId()) {
default:
break;
case R.id.btn1:
//获取用户名和密码
String mobile = mEditPhone.getText().toString();
String password = mEditPwd.getText().toString();
loginPresenter.login(mobile,password);
break;
}
}
public void loginSuccess(UserBean userBean) {
//保存用户信息到SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("user", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putString("uid", userBean.getData().getUid() + "");
edit.putString("name", userBean.getData().getUsername() + "");
edit.putString("iconUrl", userBean.getData().getIcon() + "");
edit.putString("token", userBean.getData().getToken() + "");
edit.commit();
//关闭当前页面
this.finish();
}
}
对应的接口
public interface LoginView {
void loginSuccess(UserBean userBean);
}
对应的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.com.lixiaolong_04_27_.mvp.fragment1.view.LoginActivity">
<RelativeLayout
android:id="@+id/login_title_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/cha"/>
<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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:id="@+id/btn1"
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/umeng_socialize_wechat"/>
<ImageView
android:id="@+id/login_by_qq"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/umeng_socialize_qq"/>
</LinearLayout>
</RelativeLayout>