【0023】Android基础-10-登陆案例

【0】登录案例要求及分析

 

 

【1】带有中文的字符串的处理

【出现的问题】

【解决方法】

同时,这样做的好处是在做国际化的时候更容易做;

【2】所有的控件必然具有的属性

【3】对工程的clean

【eclipse中的clean】

 

【AS中的clean】

 

【4】密码的“*”号代替

下面提示需要使用inputType来对明文的密码进行遮盖;

 【解决方法】增加属性textPassword

【5】登录按钮中的字的间距调整

 

【6】登录按钮和记住密码的checkBox的居中对齐

 

【布局源码】线性布局当中嵌套了相对布局

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin" >
10 
11     <EditText
12         android:id="@+id/et_username"
13         android:layout_width="fill_parent"
14         android:layout_height="wrap_content"
15         android:hint="@string/input_username" />
16 
17     <EditText
18         android:layout_marginTop="10dp"
19         android:layout_marginBottom="10dp"
20         android:id="@+id/et_password"
21         android:layout_width="fill_parent"
22         android:layout_height="wrap_content"
23         android:hint="@string/input_password"
24         android:inputType="textPassword" />
25 
26     <RelativeLayout
27         android:layout_width="fill_parent"
28         android:layout_height="wrap_content" >
29 
30         <CheckBox
31             android:layout_centerVertical="true"
32             android:layout_alignParentLeft="true"
33             android:id="@+id/cb_rem"
34             android:layout_width="wrap_content"
35             android:layout_height="wrap_content"
36             android:text="@string/rem_password" />
37 
38         <Button
39             android:paddingRight="50dp"
40             android:paddingLeft="50dp"
41              android:layout_centerVertical="true"
42             android:layout_alignParentRight="true"
43             android:id="@+id/bt_login"
44             android:layout_width="wrap_content"
45             android:layout_height="wrap_content"
46             android:text="@string/login" />
47     </RelativeLayout>
48 
49 </LinearLayout>

【7】Context的使用技巧-在使用context的时候,直接使用定义的mContext进行代替;

【8】用户名和密码的保存:将用户名和密码保存在data/data/包名下的txt文件中;

【9】用户名和密码的回显

 

【源码】

 

 【MainActivity.java源码】

 1 package com.itheima.login;
 2 
 3 import java.util.Map;
 4 
 5 import com.itheima.login.util.UserInfoUtil;
 6 
 7 import android.app.Activity;
 8 import android.content.Context;
 9 import android.os.Bundle;
10 import android.text.TextUtils;
11 import android.view.View;
12 import android.view.View.OnClickListener;
13 import android.widget.Button;
14 import android.widget.CheckBox;
15 import android.widget.EditText;
16 import android.widget.Toast;
17 
18 public class MainActivity extends Activity implements OnClickListener{
19 
20     private EditText et_username;
21     private EditText et_password;
22     private CheckBox cb_rem;
23     private Button bt_login;
24     private Context mContext;
25 
26     @Override
27     protected void onCreate(Bundle savedInstanceState) {
28         super.onCreate(savedInstanceState);
29         setContentView(R.layout.activity_main);
30         mContext = this;
31         et_username = (EditText) findViewById(R.id.et_username);
32         et_password = (EditText) findViewById(R.id.et_password);
33         cb_rem = (CheckBox) findViewById(R.id.cb_rem);
34         bt_login = (Button) findViewById(R.id.bt_login);
35         //b.设置按钮的点击事件
36         bt_login.setOnClickListener(this);
37 
38         
39         //f.回显用户名密码 ??
40         Map<String, String> map = UserInfoUtil.getUserInfo_android(mContext);//获取用户名密码
41         if(map != null){
42             String username = map.get("username");
43             String password = map.get("password");
44             et_username.setText(username);//设置用户名
45             et_password.setText(password);
46             cb_rem.setChecked(true);//设置复选框选中状态
47         }
48 
49     }
50 
51     private void login(){
52 
53         //c.在onclick方法中,获取用户输入的用户名密码和是否记住密码
54 
55             String username = et_username.getText().toString().trim();
56             String password = et_password.getText().toString().trim();
57             boolean isrem = cb_rem.isChecked();
58         //d.判断用户名密码是否为空,不为空请求服务器(省略,默认请求成功)
59             if(TextUtils.isEmpty(username) || TextUtils.isEmpty(password)){
60                 Toast.makeText(mContext, "用户名密码不能为空", Toast.LENGTH_SHORT).show();
61                 return ;
62             }
63             
64         //请求服务器,后面讲。。。。。。。。。。
65                 
66         //e.判断是否记住密码,如果记住,将用户名密码保存本地。???? 
67             if(isrem){
68                 boolean result = UserInfoUtil.saveUserInfo_android(mContext,username,password);
69                 if(result){
70                     Toast.makeText(mContext, "用户名密码保存成功", Toast.LENGTH_SHORT).show();
71                 }else{
72                     Toast.makeText(mContext, "用户名密码保存失败", Toast.LENGTH_SHORT).show();    
73                 }
74                 
75             }else{
76                 Toast.makeText(mContext, "无需保存", Toast.LENGTH_SHORT).show();
77             }
78 
79 
80 
81     }
82 
83     @Override
84     public void onClick(View v) {
85         switch (v.getId()) {
86         case R.id.bt_login:
87             login();
88             break;
89 
90         default:
91             break;
92         }
93     }
94 
95 
96 }

【UserInfoUtil.java源码】

  1 package com.itheima.login.util;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.FileOutputStream;
  7 import java.io.InputStreamReader;
  8 import java.util.HashMap;
  9 import java.util.Map;
 10 
 11 import android.content.Context;
 12 
 13 public class UserInfoUtil {
 14     
 15     //保存用户名密码
 16     public static boolean saveUserInfo_android(Context context,String username, String password) {
 17 
 18         try{
 19             String userinfo = username + "##"+ password;//封装用户名密码
 20             //得到私有目录下一个文件写入流; name : 私有目录文件的名称    mode: 文件的操作模式, 私有,追加,全局读,全局写
 21             FileOutputStream fileOutputStream = context.openFileOutput("userinfo.txt", Context.MODE_PRIVATE);
 22             fileOutputStream.write(userinfo.getBytes());//将用户名密码写入文件
 23             fileOutputStream.close();
 24             return true;
 25         }catch (Exception e) {
 26             e.printStackTrace();
 27         }
 28 
 29         return false;
 30     }
 31     
 32     
 33     //获取用户名密码
 34     public static Map<String ,String> getUserInfo_android(Context context){
 35         
 36         try{
 37             
 38             //通过context对象获取一个私有目录的文件读取流
 39             FileInputStream fileInputStream = context.openFileInput("userinfo.txt");
 40             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
 41             //读取一行中包含用户密码,需要解析
 42             String readLine = bufferedReader.readLine();
 43             String[] split = readLine.split("##");
 44             HashMap<String, String> hashMap = new HashMap<String ,String>();
 45             hashMap.put("username", split[0]);
 46             hashMap.put("password", split[1]);
 47             bufferedReader.close();
 48             fileInputStream.close();
 49             return hashMap;
 50             
 51         }catch (Exception e) {
 52             e.printStackTrace();
 53         }
 54         return null;
 55         
 56     }
 57     
 58     
 59     
 60     //保存用户名密码
 61     public static boolean saveUserInfo(Context context,String username, String password) {
 62 
 63         try{
 64             String userinfo = username + "##"+ password;//封装用户名密码
 65 //            String path = "/data/data/com.itheima.login/";//指定保存的路径
 66             //通过Context对象获取私有目录的一个路径
 67             String path = context.getFilesDir().getPath();
 68             System.out.println("...............:"+path);
 69             File file = new File(path,"userinfo.txt");//创建file
 70             FileOutputStream fileOutputStream = new FileOutputStream(file);//创建文件写入流
 71             fileOutputStream.write(userinfo.getBytes());//将用户名密码写入文件
 72             fileOutputStream.close();
 73             return true;
 74         }catch (Exception e) {
 75             e.printStackTrace();
 76         }
 77 
 78         return false;
 79     }
 80     
 81     //获取用户名密码
 82     public static Map<String ,String> getUserInfo(Context context){
 83         
 84         try{
 85 //            String path = "/data/data/com.itheima.login/";//指定保存的路径
 86             
 87             //通过Context对象获取私有目录的一个路径
 88             String path = context.getFilesDir().getPath();
 89             System.out.println("...............:"+path);
 90             File file = new File(path,"userinfo.txt");//创建file
 91             FileInputStream fileInputStream = new FileInputStream(file);
 92             BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
 93             //读取一行中包含用户密码,需要解析
 94             String readLine = bufferedReader.readLine();
 95             String[] split = readLine.split("##");
 96             HashMap<String, String> hashMap = new HashMap<String ,String>();
 97             hashMap.put("username", split[0]);
 98             hashMap.put("password", split[1]);
 99             bufferedReader.close();
100             fileInputStream.close();
101             return hashMap;
102             
103         }catch (Exception e) {
104             e.printStackTrace();
105         }
106         return null;
107         
108     }
109 
110 }

 【10】通过context对象获取私有目录

 【出现问题】

【解决方法】在path的获取中不要使用硬编码

 

【11】复选框的选中状态

 

posted @ 2017-10-18 21:48  OzTaking  阅读(316)  评论(0)    收藏  举报