1、在子线程中显示Toast提示
//子线程中显示Toast public void ShowToast(String str) { Looper.prepare(); Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show(); Looper.loop(); }2、界面中按返回键,提示是否退出应用
//重写onKeyDown方法
private long exitTime = 0;//主界面中按返回键,提示是否退出应用@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) { if ((System.currentTimeMillis() - exitTime) > 2000) { Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show(); exitTime = System.currentTimeMillis(); } else { finish(); System.exit(0); } return true; } return super.onKeyDown(keyCode, event); }
3、使用SharedPreferences和CheckBox保存用户名
①定义和初始化控件
private EditText loginEdit;private CheckBox loginCheck;private SharedPreferences sharedPreferences;private SharedPreferences.Editor editors; private String username;//获取到用户输入的用户名
②首先进入页面,判断LOGIN中是否有数据loginEdit= (EditText) findViewById(R.id.activity_login_username);loginCheck= (CheckBox) findViewById(R.id.activity_login_checkbox);sharedPreferences= getSharedPreferences("LOGIN_SP", MODE_PRIVATE);editors= sharedPreferences.edit();
String name= sp.getString("login", ""); if (name!= null) { loginCheck.setChecked(true); loginEdit.setText(name); }③当输入正确的用户名和密码登录之后
username= loginService.getText().toString().trim();if (loginCheck.isChecked()) { editors.putString("login", username); editors.commit(); } else { editors.remove("login"); editors.commit(); }