android登入實現,儲存資料到/data/data/包名/info.txt

來源:互聯網
上載者:User

標籤:ring   linear   cti   etc   width   market   activity   odi   final   

1.一個簡單登入介面布局代碼如下:

@1採用線性布局加相對布局方式

@2線性布局採用垂直排列

<?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:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.market.login.MainActivity">    <EditText        android:id="@+id/et_name"        android:layout_marginTop="150dp"        android:layout_width="match_parent"        android:layout_height="wrap_content" />    <EditText        android:id="@+id/et_pwd"        android:layout_marginTop="20dp"        android:layout_width="match_parent"        android:layout_height="wrap_content" />        <RelativeLayout            android:layout_width="match_parent"            android:layout_marginTop="20dp"            android:layout_height="wrap_content">            <CheckBox                android:id="@+id/cb"                android:layout_width="wrap_content"                android:layout_marginLeft="10dp"                android:layout_centerVertical="true"                android:layout_height="wrap_content"                android:text="是否記住密碼"/>            <Button                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:text="登入"                android:layout_marginRight="50dp"                android:layout_centerHorizontal="true"                android:layout_alignParentRight="true"/>        </RelativeLayout></LinearLayout>

介面效果如下:

2.代碼邏輯,分為MainActivity類和SaveUserInfo 工具類

分三步:初始化UI,初始化資料(載入),初始化控制器

@1主代碼如下

package com.market.login;import android.app.Activity;import android.os.Bundle;import android.text.TextUtils;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.CheckBox;import android.widget.EditText;import android.widget.Toast;import java.util.Map;import static com.market.login.R.id.cb;public class MainActivity extends Activity {    private Button btn;    private EditText et_name;    private EditText et_pwd;    private CheckBox cb;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initView();        loadData();    }    public void login(View v){        String name = et_name.getText().toString().trim();        String password = et_pwd.getText().toString().trim();        if(TextUtils.isEmpty(name) || TextUtils.isEmpty(password)){            Toast.makeText(this,"使用者名稱和密碼不可為空",Toast.LENGTH_SHORT).show();        }else{            if(cb.isChecked()) {//如果儲存密碼和名                //開始儲存,儲存到檔案,下次進來先讀取這個檔案                SaveUserInfo.saveInfo(name,password,true);                //提交使用者名稱和密碼給伺服器                Log.v("Maintivity","提交使用者名稱和密碼給伺服器");            }else{                SaveUserInfo.saveInfo("","",false);                //直接提交使用者名稱和密碼給伺服器                Log.v("Maintivity","直接提交使用者名稱和密碼給伺服器");            }        }    }    public void initView(){        btn = (Button)findViewById(R.id.btn);        et_name = (EditText)findViewById(R.id.et_name);        et_pwd = (EditText)findViewById(R.id.et_pwd);        cb = (CheckBox)findViewById(R.id.cb);    }    public void loadData(){        Map<String, String> info = SaveUserInfo.readInfo();        if(info != null){            et_name.setText(info.get("name"));            et_pwd.setText(info.get("password"));            cb.setChecked(info.get("isChecked").equals("true"));//如果儲存了資訊,那他上次就是勾選的        }    }}

@2儲存資料到/data/data/包名/下檔案的工具類

package com.market.login;import android.util.Log;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStreamReader;import java.util.HashMap;import java.util.Map;/** * 儲存使用者資訊的類,實現儲存使用者資訊到檔案 * Created by Administrator on 2017/6/14. */public class SaveUserInfo {    /*儲存使用者資訊    * name:使用者名稱    * password:密碼    * isChecked:是否勾選儲存密碼    *    * */    public static boolean saveInfo(String name,String pwd,boolean isChecked){        String info = name+"#"+pwd+"#"+isChecked;        File file = new File("/data/data/com.market.login/info.txt");        FileOutputStream fos = null;        try {             fos = new FileOutputStream(file);            fos.write(info.getBytes());            return true;        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            if(fos != null) {                try {                    fos.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            return false;        }    }    /*讀取使用者資訊*/    public static Map<String,String> readInfo(){        Map<String,String> map = null;        File file = new File("/data/data/com.market.login/info.txt");        if(!file.exists()){            return map;        }        FileInputStream fis = null;        BufferedReader br = null;        try {            fis = new FileInputStream(file);             br = new BufferedReader(new InputStreamReader(fis));            String info = br.readLine();            Log.e("SaveUserInfo",info);            String[] split = info.split("#");            map = new HashMap<String,String>();            map.put("name",split[0]);//儲存讀取的使用者名稱和密碼到map中            map.put("password",split[1]);            map.put("isChecked",split[2]);        } catch (FileNotFoundException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }finally {            if(fis != null) {                try {                    fis.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            if(br != null) {                try {                    br.close();                } catch (IOException e) {                    e.printStackTrace();                }            }            return map;        }    }}

3.運行效果,分三種情況

@1使用者名稱和密碼填寫有空

@2沒有勾選checkbox效果和退出後重新登入效果

   

@3勾選後效果和退出重新登入效果

  

 

  

 

android登入實現,儲存資料到/data/data/包名/info.txt

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.