登录界面
程序员文章站
2022-04-28 22:00:59
...
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; } } }