One of the ways that Android stores files---sharedpreferences content providers, storing data in an XML way. is a lightweight file data store

Source: Internet
Author: User
Tags gettext

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 //UI界面的布局 文件<br><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="fill_parent"//填充父元素  线性布局<br>    android:layout_height="fill_parent"     android:orientation="vertical" >     <EditText         android:id="@+id/UserName"//id名称方便后台获取到该控件名称来去控件里面的值<br>        android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:ems="10"         android:inputType="text" >         <requestFocus />     </EditText>     <EditText         android:id="@+id/Password"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:inputType="text" />     <LinearLayout         android:layout_width="fill_parent"         android:layout_height="wrap_content" >         <Button             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:onClick="login"//在该Button按钮上面绑定onClick()方法 login和后台中的login名称需要一直,否则将找不到后台的方法<br>            android:text="登陆"             android:width="80dp" />         <CheckBox             android:id="@+id/saveUserAndPassword"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_marginLeft="180dp" />     </LinearLayout> </LinearLayout>

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47) 48 49 package com.example.saveuserandpasswor; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.CheckBox; import android.widget.EditText; import android.widget.Toast;<br>/**<br> *模拟一个简单的用户登录时保存密码的功能。该demo 没有对密码进行加密,处于安全考虑可以使用MD5或UUID进行密码加密  --后台代码<br> */ public class MainActivity extends Activity {    private EditText userName;     private EditText password;     private CheckBox cb;     private SharedPreferences sp;// 内容提供者    @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         userName = (EditText) findViewById(R.id.UserName);//获取ui界面中的空间元素         password = (EditText) findViewById(R.id.Password);         cb = (CheckBox) findViewById(R.id.saveUserAndPassword);         sp = getSharedPreferences("save", MODE_PRIVATE);// 设置保存信息的配置文件是私有的文件,存储问文件形式以xml文件存储,其实就是一个map                                                         // 集合         String username = sp.getString("username", "");         String pass = sp.getString("password", "");         if (username.length() != 0 && pass.length() != 0) {             userName.setText(username);             password.setText(pass);             cb.setChecked(true);         }    }    public void login(View view) {//在UI界面里面对Button按钮进行事件绑定,onclick() 方法<br>     String user = userName.getText().toString();         String pass = password.getText().toString();         boolean iscb = cb.isChecked();         Editor editor = sp.edit();// 获取编辑器         if (iscb) {// 如果checkbox 被选中则保存用户名和密码             editor.putString("username", user);             editor.putString("password", pass);         } else {             editor.putString("username", "");             editor.putString("password", "");         }         editor.commit();// 登陆完事后将用户输入的账号密码保存到配置文件中         Toast.makeText(getApplicationContext(), "登陆成功", Toast.LENGTH_SHORT)// 操作完成一个时间后执行的操作                 .show();     }    @Override     public boolean onCreateOptionsMenu(Menu menu) {         getMenuInflater().inflate(R.menu.activity_main, menu);         return true;     } }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.