登录Activity

package movie.bw.com.firstweek;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.google.gson.Gson;

import movie.bw.com.firstweek.activity.RegisterActivity;
import movie.bw.com.firstweek.activity.ShowActivity;
import movie.bw.com.firstweek.bean.UserInFoBean;
import movie.bw.com.firstweek.mvp.model.MainModellml;
import movie.bw.com.firstweek.mvp.presenter.MainPresenterlml;
import movie.bw.com.firstweek.mvp.view.MainView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, MainView {

private EditText username;
private EditText password;
private Button login;
private Button register;
private SharedPreferences sp;
private MainPresenterlml mainPresenterlml;
private String name;
private String pass;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

// 初始化控件
initview();
mainPresenterlml = new MainPresenterlml(new MainModellml(), this);
sp = getSharedPreferences(“user”, MODE_PRIVATE);
}

@Override
protected void onResume() {
    super.onResume();
    String user_name = sp.getString("user_name", null);
    String user_pass = sp.getString("user_pass", null);
    if (!TextUtils.isEmpty(user_name)){
        username.setText(user_name);
    }
    if (!TextUtils.isEmpty(user_pass)){
        password.setText(user_pass);
    }
}

private void initview() {
    username = findViewById(R.id.edit_username);
    password = findViewById(R.id.edit_password);
    login = findViewById(R.id.btn_login);
    register = findViewById(R.id.btn_register);

    login.setOnClickListener(this);
    register.setOnClickListener(this);

}

// 登录注册点击
@Override
public void onClick(View v) {
switch (v.getId()){
// 登录
case R.id.btn_login:
dologin();
break;
// 注册
case R.id.btn_register:
startActivity(new Intent(MainActivity.this,RegisterActivity.class));
break;
}
}
// 登录
private void dologin() {
name = username.getText().toString().trim();
pass = password.getText().toString().trim();
if (TextUtils.isEmpty(name)){
Toast.makeText(this,“用户名不能为空”,Toast.LENGTH_LONG).show();
return;
}
if (TextUtils.isEmpty(pass)){
Toast.makeText(this,“密码不能为空”,Toast.LENGTH_LONG).show();
return;
}
// 走登录
mainPresenterlml.doLogin(name, pass);

}

// 成功
@Override
public void success(String data) {
UserInFoBean bean = new Gson().fromJson(data, UserInFoBean.class);
sp.edit().putString(“user_name”,bean.getResult().getPhone())
.putString(“user_nickName”,bean.getResult().getNickName())
.putString(“user_pictrue”,bean.getResult().getHeadPic())
.commit();
// 跳到用户展示
startActivity(new Intent(MainActivity.this,ShowActivity.class));
}
// 失败
@Override
public void fail(String error) {
Toast.makeText(this,error,Toast.LENGTH_LONG).show();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值