登录案例_android

来源:互联网 发布:数控车床编程视频教学 编辑:程序博客网 时间:2024/05/29 02:44

<span style="font-family: TimesNewRomanPSMT; font-size: 11pt;">activity_main.xml</span><span style="font-family: Arial, Helvetica, sans-serif;">1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"</span>xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><EditText android:id="@+id/et_name" android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入用户名"/> <EditTextandroid:id="@+id/et_pwd"android:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="textPassword" android:hint="请输入密码"/><RelativeLayoutandroid:layout_width="match_parent" android:layout_height="wrap_content" ><CheckBox android:id="@+id/cb"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="记住密码"android:layout_centerVertical="true"/><Buttonandroid:layout_alignParentRight="true"android:onClick="login"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="登录"/> </RelativeLayout></LinearLayout>


1、保存到data目录

MainActivity.java
* 将用户名和密码保存到 data 目录**/ public class MainActivity extends Activity { private EditText et_name; private EditText et_pwd; private CheckBox cb; @Override protected void onCreate (Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取控件对象 et_name = (EditText) findViewById(R.id.et_name); et_pwd = (EditText) findViewById(R.id.et_pwd); cb = (CheckBox) findViewById(R.id.cb); /* * 数据的回显 * getFilesDir()方法是父类提供的方法可以直接访问 data/data/包名/files 目录 */ File file = new File(getFilesDir(), "info.txt"); //如果文件存在则回显数据 if (file.exists()) { try { /* * openFileInput(String)是父类提供的方法, * 可以直接获取 data 目录下指定文件的的输入流 */ FileInputStream fis = openFileInput("info.txt"); //将文件输入流转化为缓存流 BufferedReader bf = new BufferedReader(new InputStreamReader(fis)); //因为保存时数据只有一行,因此读取一次就可以 String readLine = bf.readLine(); bf.close(); /* * 数据是用户名##密码的形式存储的, * 因此需要根据##对字符串进行切割 */ String[] split = readLine.split("##"); //给 EditText 控件设置数据 et_name.setText(split[0]); et_pwd.setText(split[1]); //让 CheckBox 变为勾选状态,默认不勾选 cb.setChecked(true); } catch (Exception e) { e.printStackTrace();    } } } /* * 给登录按钮绑定的点击事件 */ public void login(View view) { //从 EditText 中获取数据 String name = et_name.getEditableText().toString().trim(); String pwd = et_pwd.getText().toString().trim(); //校验数据 if (TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)) { /* * 这里使用到了 makeText 的方法的重载形式,第二个参数是 int 类型, * 其对应 values/strings.xml 资源文件中的字符串常量 */ Toast.makeText(this, R.string.info_txt, Toast.LENGTH_SHORT).show(); return; } //获取 CheckBox 的选中状态 boolean checked = cb.isChecked(); //保存数据 name##pwd File file = new File(getFilesDir(), "info.txt"); if (checked) { try { /* * openFileOutput()方法是父类提供的直接打开 data 目中中指定文件的输出流, * 输出地址:/data/data/包名/files/info.txt */ FileOutputStream fos = openFileOutput("info.txt", MODE_PRIVATE); fos.write((name+"##"+pwd).getBytes()); fos.close(); } catch (Exception e) {. e.printStackTrace(); } }else { //如果用户没有勾选 CheckBox 则删除历史文件 if (file.exists()) { boolean delete = file.delete(); if (delete) { Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show(); } }else { Toast.makeText(this, " 该 文 件 不 存 在 , 不 用 删 除 ",Toast.LENGTH_SHORT).show();} } }}
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">注意:</span>
上面使用到了如下方法,这些方法都是在 ContextWrapper 类中定义的,是 Activity 的父类,因此可以
直接使用。
getFilesDir():直接获取 /data/data/包名/files 文件
openFileInput("info.txt"):直接打开/data/data/包名/files/info.txt 文件
openFileOutput("info.txt", MODE_PRIVATE):直接创建 /data/data/包名/files/info.txt 文件
在开发过程中凡是涉及到 data 目录操作的,很少自己去创建输入流和输出流,而应该尽量去使用
Google 已经提供的方法。
openFileOutput("info.txt", MODE_PRIVATE)方法的第二个参数是设置文件的权限,这里使用了私有常
量值,也就是只能当前应用访问该文件


2、数据存储到 sdcard

22. public class MainActivity extends Activity {23.24. private EditText et_name;25. private EditText et_pwd;26. private CheckBox cb;27.28. @Override29. protected void onCreate(Bundle savedInstanceState) {30. super.onCreate(savedInstanceState);31. setContentView(R.layout.activity_main);32. //获取控件对象33. et_name = (EditText) findViewById(R.id.et_name);34. et_pwd = (EditText) findViewById(R.id.et_pwd);35. cb = (CheckBox) findViewById(R.id.cb);36. /*37. * 通过 Environment 工具类提供的方法获取 sdcard 根路径38. */39. File file = new File(Environment.getExternalStorageDirectory(), "info.txt");40. //实现数据的回显41. if (file.exists()) {42. try {43. FileReader reader = new FileReader(file);44. BufferedReader bf = new BufferedReader(reader);45. String readLine = bf.readLine();46. bf.close();47. // 显示48. String[] split = readLine.split("##");49. et_name.setText(split[0]);50. et_pwd.setText(split[1]);51. cb.setChecked(true);52. } catch (Exception e) {53. e.printStackTrace();54. }55. }56. }57.58. /*59. * 给登录按钮绑定的点击事件60. */61. public void login(View view) {黑马上海程序员——只要学不死,就往死里学,冲击年薪 20 万!1362. //从 EditText 中获取数据63. String name = et_name.getEditableText().toString().trim();64. String pwd = et_pwd.getText().toString().trim();65. // 校验数据66. if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {67. Toast.makeText(this, R.string.info_txt, Toast.LENGTH_SHORT).show();68. return;69. }70. // 获取 CheckBox 的选中状态71. boolean checked = cb.isChecked();72. /*73. * 使用 Environment 提供的静态方法获取 sdcard 根路径74. */75. File file = new File(Environment.getExternalStorageDirectory(), "info.txt");76. if (checked) {77. try {78. FileWriter writer = new FileWriter(file);79. writer.write(name + "##" + pwd);80. writer.close();81.82. } catch (Exception e) {83. e.printStackTrace();84. }85. } else {86. // 删除数据87. if (file.exists()) {88. boolean delete = file.delete();89. if (delete) {90. Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show();91. } else {92. Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show();93. }94. } else {95. Toast.makeText(this, "该文件不存在,不用删除", Toast.LENGTH_SHORT).show();96. }97. }98. }99. }100.

sdcard 进行了读写操作,sdcard是受保护的目录,因此需要在AndroidManifest.xml
中声明权限。
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
第一个权限是读权限,第二个是写权限。如果第二个权限声明了,通常第一个权限可以不声明。


3、将数据存储到SharedPreferences

18. */19. public class MainActivity extends Activity {20.21. private EditText et_name;22. private EditText et_pwd;23. private CheckBox cb;24. private SharedPreferences sp;25.26. @Override27. protected void onCreate(Bundle savedInstanceState) {28. super.onCreate(savedInstanceState);29. setContentView(R.layout.activity_main);30. // 获取控件31. et_name = (EditText) findViewById(R.id.et_name);32. et_pwd = (EditText) findViewById(R.id.et_pwd);33. cb = (CheckBox) findViewById(R.id.cb);34. /*35. * 调用父类的方法获取 sp 对象36. * 第一个参数:sp 文件的名字,没有则创建37. * 第二个参数:文件权限38. */39. sp = getSharedPreferences("info", MODE_PRIVATE);40.41. // 数据的回显42. // 从 sp 中获取数据43. String name = sp.getString("name", "");44. String pwd = sp.getString("pwd", "");45. // 给 EditText 设置数据46. et_name.setText(name);47. et_pwd.setText(pwd);48. }49.50. /*51. * 给登录按钮绑定的点击事件黑马上海程序员——只要学不死,就往死里学,冲击年薪 20 万!1952. */53. public void login(View view) {54. // 获取 EditText 中的数据55. String name = et_name.getEditableText().toString().trim();56. String pwd = et_pwd.getText().toString().trim();57. // 校验数据58. if (TextUtils.isEmpty(name) || TextUtils.isEmpty(pwd)) {59. Toast.makeText(this, R.string.info_txt, Toast.LENGTH_SHORT).show();60. return;61. }62. // 获取 CheckBox 的选中状态63. boolean checked = cb.isChecked();64. // 保存数据 name##pwd65. if (checked) {66. try {67. /*68. * 如果想往 sp 中添加、修改、删除数据则需要通过 sp 获取到 Editor69. */70. Editor editor = sp.edit();71. // 设置数据72. editor.putString("name", name);73. editor.putString("pwd", pwd);74. // 一定要记得执行提交方法,不然前面保存的数据没有任何效果75. editor.commit();76.77. } catch (Exception e) {78. e.printStackTrace();79. }80. } else {81. // 删除数据82. Editor edit = sp.edit();83. edit.clear();84. edit.commit();85. }86. }87. }



0 0