Android儲存資料到本地檔案

來源:互聯網
上載者:User

標籤:android 記住使用者名稱密碼 記憶體

xml檔案

<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:orientation="vertical"    android:padding="5dp" >    <EditText        android:id="@+id/qqnum"        android:layout_width="fill_parent"        android:layout_height="wrap_content"       android:hint="請輸入QQ"       android:inputType="number"        android:textSize="20dp"        />    <EditText         android:id="@+id/pass"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:hint="請輸入密碼"        android:inputType="textPassword"        android:textSize="20dp"                />    <CheckBox         android:id="@+id/rem"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="記住密碼"                />    <Button         android:id="@+id/Login"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="登入"/></LinearLayout>

Utils

package com.example.android22filelogin;import java.io.BufferedReader;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;import android.text.TextUtils;public class Utils {public static boolean saveUserInfo(String username,String pwd){String data=username+"##"+pwd;String path="/data/data/com.example.android22filelogin/data.txt";try {FileOutputStream out=new FileOutputStream(path);out.write(data.getBytes());out.flush();out.close();return true;} catch (Exception e) {e.printStackTrace();}return false;}public static Map<String,String> getUserInfo(){String path="/data/data/com.example.android22filelogin/data.txt";try {BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(path)));String data=reader.readLine();if(!TextUtils.isEmpty(data)){String [] datas=data.split("##");Map<String, String> userinfo=new HashMap<String, String>();userinfo.put("number", datas[0]);userinfo.put("pwd", datas[1]);return userinfo;}} catch (Exception e) {e.printStackTrace();}return null;}}

activity

public class MainActivity extends Activity implements OnClickListener {private EditText qqnum,pwd;private CheckBox rem;private Button but;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        qqnum=(EditText)findViewById(R.id.qqnum);        pwd=(EditText)findViewById(R.id.pass);        rem=(CheckBox)findViewById(R.id.rem);        but=(Button)findViewById(R.id.Login);        but.setOnClickListener(this);        //回顯資料        Map<String, String> userinfo=Utils.getUserInfo();        if(userinfo!=null)        {        qqnum.setText(userinfo.get("number"));        pwd.setText(userinfo.get("pwd"));        }    }@Overridepublic void onClick(View v) {//記住號碼和密碼String num=qqnum.getText().toString();String password=pwd.getText().toString();if(TextUtils.isEmpty(num)||TextUtils.isEmpty(password)){Toast.makeText(this, "使用者名稱或密碼不可為空", Toast.LENGTH_LONG).show();return;}//判斷是否記住密碼if(rem.isChecked()){boolean isSuccess=Utils.saveUserInfo(num, password);Toast.makeText(this, isSuccess+"", Toast.LENGTH_LONG).show();}//登入成功}}


本文出自 “Java大白的戰地” 部落格,請務必保留此出處http://8023java.blog.51cto.com/10117207/1664699

Android儲存資料到本地檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.