Android实现记住用户名和密码功能
作者:GXSeveryday 时间:2023-10-06 13:02:07
Android 实现记住用户名和密码的功能是通过SharedPreference 存储来实现的。创建一个复选按钮,通过按钮的否选取来进行事件处理。若按钮选中存储账号和密码的信息。若按钮没有选中,则清空账号和密码的信息。
结果演示:
源代码下载地址:
https://github.com/GXS1225/Android————-.git
分析
(1)判断是否输入了账号和密码
if(name.trim().equals("")){
Toast.makeText(this, "请您输入用户名!", Toast.LENGTH_SHORT).show();
return;
}
if(pswd.trim().equals("")){
Toast.makeText(this, "请您输入密码!", Toast.LENGTH_SHORT).show();
return;
}
(2)在layout_main.xml定义一个 CheckBox,进行事件处理
//通过
boolean CheckBoxLogin = checkbox.isChecked();
//按钮被选中,下次进入时会显示账号和密码
if (CheckBoxLogin)
{
Editor editor = sp.edit();
editor.putString("uname", name);
editor.putString("upswd", pswd);
editor.putBoolean("auto", true);
editor.commit();
}
//按钮被选中,清空账号和密码,下次进入时会显示账号和密码
else
{
Editor editor = sp.edit();
editor.putString("uname", null);
editor.putString("upswd", null);
editor.putBoolean("auto", false);
editor.commit();
}
(3) SharedPreference 的存储实现
//先定义
SharedPreferences sp = null;
sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
//对uname 和 upswd 的操作
if (sp.getBoolean("checkboxBoolean", false))
{
uname.setText(sp.getString("uname", null));
upswd.setText(sp.getString("upswd", null));
checkboxButton.setChecked(true);
}
(4)跳转到Content.java界面
//Intent跳转
Intent intent = new Intent(Welcome.this,Content.class);
startActivity(intent);
finish();
步骤:
先写一个登陆的界面: layout_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ADD8E6">
<RelativeLayout
android:id="@+id/login_div"
android:layout_width="fill_parent"
android:layout_height="221dp"
android:layout_margin="15dip"
android:background="@drawable/btn_bg"
android:padding="15dip" >
<TextView
android:id="@+id/login_user_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_margin="5dp"
android:text="@string/user"
android:textSize="16dp"
android:typeface="sans" />
<EditText
android:id="@+id/user_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/login_user_input"
android:background="@android:drawable/editbox_background"
android:inputType="text"
android:singleLine="true" />
<TextView
android:id="@+id/login_pass_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/user_input"
android:layout_margin="5dp"
android:text="@string/pass"
android:textSize="16dp"
android:typeface="sans" />
<EditText
android:id="@+id/pass_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/login_pass_input"
android:background="@android:drawable/editbox_background"
android:inputType="textPassword"
android:singleLine="true" />
<CheckBox
android:id="@+id/checkBoxLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/pass_input"
android:layout_alignParentBottom="true"
android:text="@string/no_user" />
<Button
android:id="@+id/new_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/pass_input"
android:layout_marginRight="28dp"
android:onClick="To_Title"
android:text="@string/new_user" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="402dp"
android:layout_height="51dp"
android:layout_marginLeft="50dp"
android:background="@drawable/gxs_ziti" />
<Button
android:layout_width="120dp"
android:layout_height="120dp"
android:onClick="To_fruist"
android:background="@drawable/gxs2"
android:layout_marginLeft="80dp"
/>
</LinearLayout>
</LinearLayout>
Welcome.java
package com.gxs.login;
import com.example.login.R;
import com.gxs.listview.*;
import android.os.Bundle;
import android.preference.Preference;
import android.app.Activity;
import android.app.SearchManager.OnCancelListener;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class Welcome extends Activity implements OnClickListener{
private EditText uname = null;
private EditText upswd = null;
private CheckBox checkboxButton = null;
private Button login = null;
SharedPreferences sp = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
sp = this.getSharedPreferences("userinfo", Context.MODE_PRIVATE);
init();
}
public void init()
{
uname = (EditText) findViewById(R.id.user_input);
upswd = (EditText) findViewById(R.id.pass_input);
checkboxButton = (CheckBox) findViewById(R.id.checkBoxLogin);
login = (Button) findViewById(R.id.new_user);
if (sp.getBoolean("checkboxBoolean", false))
{
uname.setText(sp.getString("uname", null));
upswd.setText(sp.getString("upswd", null));
checkboxButton.setChecked(true);
}
login.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == login){
String name = uname.getText().toString();
String pswd = upswd.getText().toString();
if(name.trim().equals("")){
Toast.makeText(this,
"请您输入用户名!", Toast.LENGTH_SHORT).show();
return;
}
if(pswd.trim().equals("")){
Toast.makeText(this,
"请您输入密码!", Toast.LENGTH_SHORT).show();
return;
}
boolean CheckBoxLogin = checkboxButton.isChecked();
if (CheckBoxLogin)
{
Editor editor = sp.edit();
editor.putString("uname", name);
editor.putString("upswd", pswd);
editor.putBoolean("checkboxBoolean", true);
editor.commit();
}
else
{
Editor editor = sp.edit();
editor.putString("uname", null);
editor.putString("upswd", null);
editor.putBoolean("checkboxBoolean", false);
editor.commit();
}
//Intent跳转
Intent intent=new Intent(Welcome.this,Content.class);
startActivity(intent);
finish();
}
}
}
Content.java
package com.gxs.listview;
import java.util.List;
import com.example.login.R;
import com.gxs.*;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Content extends Activity{
private ListView listview_fruits;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.content);
}
}
content.xml
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Welcome"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="内容"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
更多内容请参考专题:Android密码使用教程
标签:Android,用户名,密码
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android中ActionBar以及menu的代码设置样式
2023-11-24 03:34:33
![](https://img.aspxhome.com/file/2023/2/106032_0s.png)
C#实现IDisposable接口释放非托管资源
2022-12-01 06:59:53
Android Studio连接手机设备教程
2021-06-10 14:40:23
![](https://img.aspxhome.com/file/2023/0/97850_0s.png)
Java8新特性Stream流实例详解
2023-05-04 12:55:05
详解C#编程获取资源文件中图片的方法
2023-06-17 23:58:51
Mybatis-plus实现主键自增和自动注入时间的示例代码
2022-11-05 00:22:51
Java实现经典游戏复杂迷宫
2021-05-29 22:33:34
![](https://img.aspxhome.com/file/2023/1/63301_0s.png)
java15新功能的详细讲解
2023-08-23 04:40:21
java split用法详解及实例代码
2022-06-27 06:48:19
java的内部类和外部类用法讲解
2022-10-18 21:14:41
![](https://img.aspxhome.com/file/2023/2/110822_0s.jpg)
JavaWeb之Filter过滤器详解
2021-06-30 03:40:55
![](https://img.aspxhome.com/file/2023/9/116099_0s.png)
No ‘Access-Control-Allow-Origin‘ header is present跨域及解决
2022-07-12 13:33:08
![](https://img.aspxhome.com/file/2023/7/71817_0s.png)
C#中泛型容器Stack<T>的用法并实现”撤销/重做”功能
2021-06-27 04:12:00
SpringSecurity整合jwt权限认证的全流程讲解
2022-02-20 09:58:36
![](https://img.aspxhome.com/file/2023/0/113200_0s.png)
浅谈C# winForm 窗体闪烁的问题
2023-06-10 09:46:07
c++禁止函数的传值调用的方法
2022-12-02 13:35:09
java 避免出现NullPointerException(空指针)的方法总结
2022-08-31 04:15:51
C#设置页面单位和缩放的方法
2023-06-09 12:59:27
AndroidStudio项目打包成jar的简单方法
2023-07-07 05:33:27
![](https://img.aspxhome.com/file/2023/0/96810_0s.png)
Java如何实现List自定义排序
2021-07-03 06:09:15