登录界面
16lz
2021-01-26
阅读更多
public class MainActivity extends Activity {private EditText user_name;private EditText user_password;private Button loginButton;private CheckBox save_pass;@Overrideprotected 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);Mapmap = LoginServices.getUserInfo(this);if(map!=null){user_name.setText(map.get("username"));user_password.setText(map.get("userpass"));}loginButton.setOnClickListener(new OnClickListener() {@Overridepublic 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 MapgetUserInfo(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 map = new HashMap ();map.put("username", infos[0]);map.put("userpass", infos[1]);return map;} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();return null;}}}
- Login.rar (986.6 KB)
- 下载次数: 1
更多相关文章
- 自写登录页面空间水平、垂直居中xml配置文件
- Android:使用密码技术安全地保存凭证
- Android表单获取值的三种方式
- Eclipse Indigo - Cannot install Android(安卓)ADT Plugin
- Android(安卓)EditText用户友好的输入界面
- Notification应用
- Gradle-Android打包文档
- android自定义弹出层
- Android(安卓)WebView设置代理及账号密码