登录界面

public class MainActivity extends Activity {

	private EditText user_name;
	private EditText user_password;
	private Button loginButton;
	private CheckBox save_pass;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		user_name = (EditText)findViewById(R.id.user_name);
		user_password = (EditText) findViewById(R.id.user_password);
		loginButton = (Button) findViewById(R.id.login);
		save_pass = (CheckBox) findViewById(R.id.save_info);
		
		Map<String, String> map = LoginServices.getUserInfo(this);
		if(map!=null){
			user_name.setText(map.get("username"));
			user_password.setText(map.get("userpass"));
		}
		
		loginButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String name = user_name.getText().toString().trim();
				String password = user_password.getText().toString().trim();
				Log.v("flag", name);
				Log.v("flag", password);
				if(TextUtils.isEmpty(name)||TextUtils.isEmpty(password)){
					Toast.makeText(MainActivity.this,
							"用户名或密码不能为空", 
							Toast.LENGTH_LONG).show();
				}
				else{
					if(save_pass.isChecked()){
						boolean res = LoginServices.saveInfo(MainActivity.this, name, password);
						
						if(res){
							Toast.makeText(MainActivity.this, 
								"Info saved",
								Toast.LENGTH_LONG).show();
						}
					}
					
					if("zhangsan".equals(name)&&"123".equals(password)){
						Toast.makeText(MainActivity.this, 
								"Login successfully", 
								Toast.LENGTH_LONG).show();
					}
					else{
						Toast.makeText(MainActivity.this,
								"Login failedly",
								Toast.LENGTH_LONG).show();
					}
				}
			}
		});
		
		
	}

}

 

 

public class  LoginServices {

	public static boolean saveInfo(Context context, String userName, String userPass){
		File file = new File(context.getFilesDir(), "info.txt");
		try {
			FileOutputStream fos = new FileOutputStream(file);
			
			fos.write((userName+"##"+userPass).getBytes());
			fos.close();
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static Map<String,String> getUserInfo(Context context){
		File file = new File(context.getFilesDir(), "info.txt");
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
			String str = br.readLine();
			String[] infos = str.split("##");
			Map<String, String> map = new HashMap<String, String>();
			map.put("username", infos[0]);
			map.put("userpass", infos[1]);
			return map;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值