Android中用SharedPerferences实现记住密码和账号的功能效果

本文介绍了如何利用Android的SharedPerferences实现记住账号和密码的功能。通过学习和实践,作者解决了遇到的问题,并分享了关键代码片段,以帮助读者更好地理解和应用SharedPerferences。
摘要由CSDN通过智能技术生成

      学习了SharedPerferences之后,我简单地制作了实现记住密码和账号的功能效果,总的来说收货还是比较多的,在老师的帮助下,我解决了一个又一个疑难问题,下面是我制作这个项目用到的代码,希望能够帮到你们更好地完成项目!

1. sh.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"
   >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
     <TextView
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:layout_weight="2"
         android:text="用户名"
         android:textSize="16sp"
        />
        <EditText
            android:id="@+id/edit_userinfo"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="8"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width=
以下是一个示例代码,使用shardpreferance保存账号密码: 1. 首先,在AndroidManifest.xml文件中添加以下权限: ```xml <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` 2. 然后,在MainActivity.java文件中添加以下代码: ```java public class MainActivity extends AppCompatActivity { private EditText etUsername; private EditText etPassword; private Button btnSave; private Button btnLoad; private SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etUsername = findViewById(R.id.et_username); etPassword = findViewById(R.id.et_password); btnSave = findViewById(R.id.btn_save); btnLoad = findViewById(R.id.btn_load); sharedPreferences = getSharedPreferences("myPreferences", Context.MODE_PRIVATE); btnSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putString("username", etUsername.getText().toString()); editor.putString("password", etPassword.getText().toString()); editor.apply(); Toast.makeText(MainActivity.this, "保存成功!", Toast.LENGTH_SHORT).show(); } }); btnLoad.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String username = sharedPreferences.getString("username", ""); String password = sharedPreferences.getString("password", ""); etUsername.setText(username); etPassword.setText(password); Toast.makeText(MainActivity.this, "读取成功!", Toast.LENGTH_SHORT).show(); } }); } } ``` 3. 最后,在activity_main.xml文件中添加以下布局: ```xml <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="16dp"> <EditText android:id="@+id/et_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入用户名"/> <EditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入密码" android:inputType="textPassword"/> <Button android:id="@+id/btn_save" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="保存"/> <Button android:id="@+id/btn_load" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="读取"/> </LinearLayout> ``` 以上代码实现了保存账号密码,并在下次打开应用程序时自动加载它们。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值