登陸案例——密碼回顯

package com.example.pengtuanyuan.logindemo01;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;


public class UserInfoUtil {
    public static Boolean saveUserInfo(String useName, String usePassword) {


        try {
            String userInfo=useName+"##"+usePassword;
            String path="/data/data/com.example.pengtuanyuan.logindemo01";
            File file=new File(path,"userInfo.txt");
            FileOutputStream fileOutputStream=new FileOutputStream(file);
            fileOutputStream.write(userInfo.getBytes());
            fileOutputStream.close();
            return true;

        } catch (IOException e) {
            e.printStackTrace();
        }

        return false;
    }
    public static Map<String,String> getUserInfo(){

        try {
            String path="/data/data/com.example.pengtuanyuan.logindemo01";
            File file=new File(path,"userInfo.txt");
            FileInputStream fileInputStream=new FileInputStream(file);
            BufferedReader bufferedReader= new BufferedReader(new InputStreamReader(fileInputStream));
            String readLine =bufferedReader.readLine();
            String[] split=readLine.split("##");
            HashMap<String,String> hashMap=new HashMap<String,String>();

            hashMap.put("useName",split[0]);
            hashMap.put("usePassword",split[1]);
            bufferedReader.close();
            fileInputStream.close();

            return hashMap;


        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;


    }
}
----------------------------------------------------
package com.example.pengtuanyuan.logindemo01;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import java.util.Map;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText ed_inputName;
    private EditText ed_inputPassword;
    private CheckBox cb_choice;
    private Button bt_login;
    private Context mContext;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        mContext=this;

        ed_inputName = (EditText) findViewById(R.id.ed_inputName);
        ed_inputPassword = (EditText) findViewById(R.id.ed_inputPassword);
        cb_choice = (CheckBox) findViewById(R.id.cb_choice);
        bt_login = (Button) findViewById(R.id.bt_login);

        bt_login.setOnClickListener(this);


        Map<String,String> map=UserInfoUtil.getUserInfo();
        if (map!=null){
            String userName=map.get("useName");
            String password=map.get("usePassword");
            ed_inputName.setText(userName);
            ed_inputPassword.setText(password);
        }
    }

    private void login(){

        String useName = ed_inputName.getText().toString().trim();
        String usePassword = ed_inputPassword.getText().toString().trim();
        boolean isChecked=cb_choice.isChecked();

        if (TextUtils.isEmpty(useName)||TextUtils.isEmpty(usePassword)){
            Toast.makeText(mContext,"Name and password cannot be empty",Toast.LENGTH_SHORT).show();
            return;
        }
        if (isChecked){
            Boolean result=UserInfoUtil.saveUserInfo(useName,usePassword);
            if (result){
                Toast.makeText(mContext,"Name and password be saved",Toast.LENGTH_SHORT).show();
            }else{
                Toast.makeText(mContext,"Name and password cannot be saved",Toast.LENGTH_SHORT).show();
            }
        }else {
            Toast.makeText(mContext,"Name and password don't need to be saved",Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.bt_login:
                login();
                break;
            default:
                break;
        }

    }
}

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末吮便,一起剝皮案震驚了整個濱河市胶哲,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌砚著,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,681評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件隅忿,死亡現(xiàn)場離奇詭異夷野,居然都是意外死亡,警方通過查閱死者的電腦和手機测秸,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來灾常,“玉大人霎冯,你說我怎么就攤上這事〕伲” “怎么了沈撞?”我有些...
    開封第一講書人閱讀 169,421評論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長雕什。 經(jīng)常有香客問我缠俺,道長拧廊,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,114評論 1 300
  • 正文 為了忘掉前任晋修,我火速辦了婚禮,結果婚禮上凰盔,老公的妹妹穿的比我還像新娘墓卦。我一直安慰自己,他們只是感情好户敬,可當我...
    茶點故事閱讀 69,116評論 6 398
  • 文/花漫 我一把揭開白布落剪。 她就那樣靜靜地躺著,像睡著了一般尿庐。 火紅的嫁衣襯著肌膚如雪忠怖。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,713評論 1 312
  • 那天抄瑟,我揣著相機與錄音凡泣,去河邊找鬼。 笑死皮假,一個胖子當著我的面吹牛鞋拟,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播惹资,決...
    沈念sama閱讀 41,170評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼贺纲,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了褪测?” 一聲冷哼從身側響起猴誊,我...
    開封第一講書人閱讀 40,116評論 0 277
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎侮措,沒想到半個月后懈叹,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,651評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡萝毛,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,714評論 3 342
  • 正文 我和宋清朗相戀三年项阴,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片笆包。...
    茶點故事閱讀 40,865評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡环揽,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出庵佣,到底是詐尸還是另有隱情歉胶,我是刑警寧澤,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布巴粪,位于F島的核電站通今,受9級特大地震影響粥谬,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜辫塌,卻給世界環(huán)境...
    茶點故事閱讀 42,211評論 3 336
  • 文/蒙蒙 一漏策、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧臼氨,春花似錦掺喻、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至持隧,卻和暖如春即硼,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背屡拨。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評論 1 274
  • 我被黑心中介騙來泰國打工只酥, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人呀狼。 一個月前我還...
    沈念sama閱讀 49,299評論 3 379
  • 正文 我出身青樓层皱,卻偏偏與公主長得像,于是被迫代替她去往敵國和親赠潦。 傳聞我的和親對象是個殘疾皇子叫胖,可洞房花燭夜當晚...
    茶點故事閱讀 45,870評論 2 361

推薦閱讀更多精彩內(nèi)容