Android——SharedPreferences存储(作业)

作业:制作一个登录界面,以SP方式存储用户名。用户下次登录时自动显示上次填写的用户名

layout文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp3.PractiseActivity"
11     android:orientation="vertical">
12 
13     <LinearLayout
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:orientation="horizontal">
17     <TextView
18         android:layout_width="wrap_content"
19         android:layout_height="wrap_content"
20         android:text="用户名:"/>
21     <EditText
22         android:layout_width="match_parent"
23         android:layout_height="wrap_content"
24         android:id="@+id/et_pt_1"/>
25     </LinearLayout>
26     <LinearLayout
27         android:layout_width="match_parent"
28         android:layout_height="wrap_content"
29         android:orientation="horizontal">
30         <TextView
31             android:layout_width="wrap_content"
32             android:layout_height="wrap_content"
33             android:text="密码:"/>
34         <EditText
35             android:layout_width="match_parent"
36             android:layout_height="wrap_content"
37             android:id="@+id/et_pt_2"
38             android:inputType="textPassword"/>
39     </LinearLayout>
40     <Button
41         android:layout_width="match_parent"
42         android:layout_height="wrap_content"
43         android:text="登陆"
44         android:onClick="bt_pt_onClick"/>
45 </LinearLayout>

java类:

 1 package com.hanqi.testapp3;
 2 
 3 import android.content.SharedPreferences;
 4 import android.os.Bundle;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.view.View;
 7 import android.widget.EditText;
 8 import android.widget.Toast;
 9 
10 public class PractiseActivity extends AppCompatActivity {
11 EditText et_pt_1;
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         setContentView(R.layout.activity_practise);
16         et_pt_1 = (EditText)findViewById(R.id.et_pt_1);
17 
18         SharedPreferences sp = getSharedPreferences("abcd",MODE_APPEND);
19         String str = sp.getString("a",null);
20         et_pt_1.setText(str);
21     }
22     public void bt_pt_onClick(View v)
23     {
24         EditText et_pt_1 = (EditText)findViewById(R.id.et_pt_1);
25         String string1 = et_pt_1.getText().toString();
26         if(string1 ==null||string1.trim().length()==0)
27         {
28             Toast.makeText(PractiseActivity.this, "请正确填写用户名称", Toast.LENGTH_SHORT).show();
29             return;
30         }
31         else
32         {
33             SharedPreferences sharedPreferences = getSharedPreferences("abcd", MODE_APPEND);
34             SharedPreferences.Editor editor = sharedPreferences.edit();
35             editor.putString("a", string1);
36             editor.commit();
37             Toast.makeText(PractiseActivity.this, "保存数据成功", Toast.LENGTH_SHORT).show();
38         }
39     }
40 }

 

效果为:

不输入用户名和密码:

随机输入用户名和密码:

退出应用:

打开应用后出现输入的用户名:

转载于:https://www.cnblogs.com/hanazawalove/p/5520502.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值