您的位置:首页 > 其它

用户登录流程中,记住密码和自动登录逻辑。

2016-12-16 13:43 519 查看
就是普通登录流程中记住密码和自动登录的功能,因为感觉checkbox  控件太小点击有点困难。所以在外层的父控件上添加了点击监听。直接上代码LoginActivity:
/**
* 登录页面
*
* @author hjl
*         created at 2016/12/15 13:51
*/
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
private SharedPreferences sp;
private SharedPreferences.Editor editor;

private EditText edittext_user;
private EditText edittext_psd;
private Button login;
private CheckBox check_remember;
private CheckBox check_automatic;
private LinearLayout automatic_login;
private LinearLayout remember_psd;
private String userNameValue, passwordValue;

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

private void initView() {
sp = this.getSharedPreferences("person", MODE_PRIVATE);// 初始化 SharedPreferences 储存
editor = sp.edit();//将SharedPreferences 储存 可编辑化

edittext_user = ((EditText) findViewById(R.id.EditText_user));//用户名输入框
edittext_psd = ((EditText) findViewById(R.id.EditText_psd));//密码输入框
login = ((Button) findViewById(R.id.button_login));//登录
check_remember = ((CheckBox) findViewById(R.id.checkbox_one));//记住密码
check_automatic = ((CheckBox) findViewById(R.id.checkbox_two));//重新登录
automatic_login = ((LinearLayout) findViewById(R.id.layout_automatic_login));
remember_psd = ((LinearLayout) findViewById(R.id.layout_remember_psd));

//判断记住密码多选框的状态
if (sp.getBoolean("ISCHECK", false)) {
//设置默认是记录密码状态
check_remember.setChecked(true);
edittext_user.setText(sp.getString("USER_NAME", ""));
edittext_psd.setText(sp.getString("PASSWORD", ""));
//判断自动登陆多选框状态
if (sp.getBoolean("AUTO_I
4000
SCHECK", false)) {
//设置默认是自动登录状态
check_automatic.setChecked(true);
//跳转界面
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(intent);
finish();
}
}

//添加点击监听的接口回调
login.setOnClickListener(this);
automatic_login.setOnClickListener(this);
remember_psd.setOnClickListener(this);
}
@Override
public void onClick(View v) {

userNameValue = edittext_user.getText().toString();
passwordValue = edittext_psd.getText().toString();
//请求借口进行登录 这里直接默认 账号hjl  密码123:
switch (v.getId()) {
case R.id.button_login:
if (userNameValue.endsWith("hjl") && passwordValue.endsWith("123")) {
ToastUtils.toastShort("login");
if (check_remember.isChecked()) {
//记住用户名、密码、
editor.putString("USER_NAME", userNameValue);
editor.putString("PASSWORD", passwordValue);
editor.commit();
}
//跳转界面
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
finish();
} else {
ToastUtils.toastShort("用户名和密码错误");
}
break;
case R.id.layout_automatic_login:
if (check_automatic.isChecked()) {
check_automatic.setChecked(false);
ToastUtils.toastShort("自动登录未选中");
sp.edit().putBoolean("AUTO_ISCHECK", false).commit();
} else {
check_automatic.setChecked(true);
ToastUtils.toastShort("自动登录选中");
sp.edit().putBoolean("AUTO_ISCHECK", true).commit();
}
break;
case R.id.layout_remember_psd:
if (check_remember.isChecked()) {
check_remember.setChecked(false);
ToastUtils.toastShort("记住密码未选中");
sp.edit().putBoolean("ISCHECK", false).commit();
} else {
check_remember.setChecked(true);
ToastUtils.toastShort("记住密码选中");
sp.edit().putBoolean("ISCHECK", true).commit();
}
break;
}
}
}
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg_login"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context="com.kenfeng.kfhjlr.kfzy.Activity.Activity.LoginActivity">

<include layout="@layout/topbar_item_copy" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:orientation="vertical">

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:gravity="center"
android:orientation="horizontal">

<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="20dp"
android:src="@mipmap/login_user" />

<EditText
android:id="@+id/EditText_user"
style="@style/EditText_style"
android:hint="请输入账号"
android:paddingLeft="30dp" />
</LinearLayout>

<View
android:layout_width="250dp"
android:layout_height="1dp"
android:background="@color/white" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:gravity="center"
android:orientation="horizontal">

<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_marginRight="20dp"
android:src="@mipmap/login_psd" />

<EditText
android:id="@+id/EditText_psd"
style="@style/EditText_style"
android:hint="请输入密码"
android:paddingLeft="30dp"
android:password="true" />
</LinearLayout>

<View
android:layout_width="250dp"
android:layout_height="1dp"
android:background="@color/white" />

<Button
android:id="@+id/button_login"
android:layout_width="260dp"
android:layout_height="43dp"
android:layout_marginTop="50dp"
android:background="@drawable/bg_button_selector"
android:text="登录"
android:textColor="@drawable/bg_button_textcolor_selector"
android:textSize="18sp" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="30dp"
android:gravity="center_vertical"
android:orientation="horizontal">

<LinearLayout
android:id="@+id/layout_remember_psd"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">

<CheckBox
android:id="@+id/checkbox_one"
android:layout_width="20dp"
android:layout_height="20dp"
android:button="@drawable/checkbox" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="记住密码"
android:textColor="@color/white"
android:textSize="18dp" />
</LinearLayout>

<View
android:layout_width="1dp"
android:layout_height="20dp"
android:background="@color/white" />

<LinearLayout
android:id="@+id/layout_automatic_login"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">

<CheckBox
android:id="@+id/checkbox_two"
android:layout_width="20dp"
android:layout_height="20dp"
android:button="@drawable/checkbox" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="自动登录"
android:textColor="@color/white"
android:textSize="18dp" />
</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="180dp"
android:gravity="center_horizontal"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="版本号:v2.9"
android:textColor="@color/white"
android:textSize="18sp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="版权说明:*****"
android:textColor="@color/white"
android:textSize="16sp" />
</LinearLayout>
</LinearLayout>
参考了:http://blog.csdn.net/liuyiming_/article/details/7704923  新手勿喷。
参考了:http://blog.csdn.net/liuyiming_/article/details/7704923  新手勿喷。
参考了:http://blog.csdn.net/liuyiming_/article/details/7704923新手勿喷。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: