package com.example.pengtuanyuan.logindemo;
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 input_name;
private EditText input_password;
private CheckBox checkBox1;
private Button button1;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
input_name = (EditText) findViewById(R.id.ed_input_name);
input_password = (EditText) findViewById(R.id.ed_input_password);
checkBox1 = (CheckBox) findViewById(R.id.checkbox1);
button1 = (Button)findViewById(R.id.button1);
button1.setOnClickListener(this);
Map<String,String>map=UserInfoUtil.getUserInfo();
if (map!=null){
String username=map.get("username");
String password=map.get("password");
input_name.setText(username);
input_password.setText(password);
}
}
private void login(){
String username=input_name.getText().toString().trim();
String password=input_password.getText().toString().trim();
boolean isChecked=checkBox1.isChecked();
if(TextUtils.isEmpty(username)||TextUtils.isEmpty(password)){
Toast.makeText(mContext,"Name and password cannot be empty",Toast.LENGTH_SHORT).show();
return;
}
if (isChecked) {
boolean result = UserInfoUtil.saveUserInfo(username, password);
if (result) {
Toast.makeText(mContext, "Name and password is saved", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(mContext, "Name and password save failed", Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(mContext, "Name and password not need to save", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.button1:
login();
break;
default:
break;
}
}
}
-----------------------------------------------------
package com.example.pengtuanyuan.logindemo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class UserInfoUtil {
public static boolean saveUserInfo(String username, String password) {
try {
String userinfo=username+"##"+password;
String path="/data/data/com.example.pengtuanyuan.logindemo/";
File file=new File(path,"userinfo.txt");
FileOutputStream fileOutStream =new FileOutputStream(file);
fileOutStream.write(userinfo.getBytes());
fileOutStream.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
public static Map<String,String>getUserInfo(){
try {
String path="/data/data/com.example.pengtuanyuan.logindemo/";
File file=new File(path,"userinfo.txt");
FileInputStream fileInputStream=new FileInputStream(file);
BufferedReader bufferReader= new BufferedReader(new InputStreamReader(fileInputStream));
String readLine=bufferReader.readLine();
String[] split=readLine.split("##");
HashMap<String,String> hashMap=new HashMap<String,String>();
hashMap.put("username",split[0]);
hashMap.put("password",split[1]);
bufferReader.close();
fileInputStream.close();
return hashMap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
-----------------------------------------------------------
登陆窗口
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 修改 /etc/lightdm/lightdm.conf [SeatDefaults] autologin-gue...
- 近日,由张介公博士主讲、喜马拉雅电台协助发行,全国各地的学子纷纷号召支持下,集科学教育记忆题材于一体的大型史诗教育...