zoukankan      html  css  js  c++  java
  • 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 }

    效果为:

    不输入用户名和密码:

    随机输入用户名和密码:

    退出应用:

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

  • 相关阅读:
    Linux pwn入门教程(1)——栈溢出基础
    Java代码审计入门篇
    利用Burp Suite攻击Web应用
    记一次对某企业的渗透测试实战
    Python 绝技 —— UDP 服务器与客户端
    SQL注入之重新认识
    文件上传和WAF的攻与防
    phpMyAdmin 4.7.x CSRF 漏洞利用
    Powershell渗透测试系列–进阶篇
    AFN检測网络情况
  • 原文地址:https://www.cnblogs.com/hanazawalove/p/5520502.html
Copyright © 2011-2022 走看看