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;
}
}
}
登陆案例——密码回显
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 【一】win10程序闪退问题,折腾了2天终于得以解决,中间各种方法都尝试了,后面使用了某驱动更新软件(有许多都更新...
- 1. ssh-keygen -t rsa 生成公钥匙与私钥匙,会生成在~/.ssh目录下 2. 免密码远程ssh:...
- 看美剧可以学好英语? 呵呵。。。。。。 看了这么多年的国产剧我也没有学好中文啊! 英语学习可以说是一件非常枯燥的事...