zoukankan      html  css  js  c++  java
  • 以SP方式存储用户名

    layout代码:

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        android:orientation="vertical"
        tools:context="com.example.my.testapp.Test_Delu">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="用户名:"/>
            <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/et_1"/>
    
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="密码:" />
            <EditText
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:id="@+id/et_2"
                android:inputType="textPassword"/>
    
        </LinearLayout>
        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="登录"
            android:onClick="bt2_onClick"/>
        </LinearLayout>
    View Code

    Activity代码:

    package com.example.my.testapp;
    
    import android.content.SharedPreferences;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class Test_Delu extends AppCompatActivity {
    
        EditText et_1,et_2;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_test__delu);
            et_1=(EditText)findViewById(R.id.et_1);
            et_2=(EditText)findViewById(R.id.et_2);
            SharedPreferences sd=getSharedPreferences("用户名", MODE_PRIVATE);
    
            String c=sd.getString("用户名",null);
    
                et_1.setText(c);
    
    
        }
        public void bt2_onClick(View v)
        {
            EditText et_1=(EditText)findViewById(R.id.et_1);
            EditText et_2=(EditText)findViewById(R.id.et_1);
            String a=et_1.getText().toString();
            String b=et_2.getText().toString();
    
            if (a==null||a.trim().length()==0||b==null||b.trim().length()==0)
            {
                Toast.makeText(Test_Delu.this, "请正确输入用户名或密码", Toast.LENGTH_SHORT).show();
            }
            else
            {
                SharedPreferences sp=getSharedPreferences("用户名", MODE_APPEND);
                SharedPreferences.Editor editor=sp.edit();
    
                editor.putString("用户名",a);
                editor.commit();
                Toast.makeText(Test_Delu.this, "登录成功。", Toast.LENGTH_SHORT).show();
            }
        }
    }
    View Code
  • 相关阅读:
    王者齐聚!Unite 2017 Shanghai 日程讲师全揭晓
    微软在.NET官网上线.NET 架构指南频道
    期待微软平台即服务技术Service Fabric 开源
    Visual Studio 20周年软件趋势随想
    .NET 十五岁,谈谈我眼中的.NET
    API网关Ocelot 使用Polly 处理部分失败问题
    互联网背景下知识半衰期这么短,如何学习?
    CentOS 7 上面安装PowerShell
    搭建consul 集群
    Entity Framework Core 实现MySQL 的TimeStamp/RowVersion 并发控制
  • 原文地址:https://www.cnblogs.com/beens/p/5520610.html
Copyright © 2011-2022 走看看