登錄頁面(動態地與數據庫匹配用戶信息)


初衷:寫這個功能主要是發現很多人寫注冊登錄都把賬號密碼寫死,不能動態更新數據,讓我很不喜歡,根本滿足不了需求,

而且第一次安裝的人,如果不知道賬號和密碼,就無法登錄,所以我覺得寫一個可以注冊用戶並登錄的應用勢在必行。

優點: 這是一個比較完善的通過注冊用戶名和密碼,實時地更新數據庫中用戶名和密碼信息,然后你在登錄頁面,輸入用戶名和密碼

,查詢是否在數據庫中能找到已有的信息與之匹配,若匹配,則可以登錄主頁面,若不能匹配則說明不存在該用戶,提示登錄失敗。

本博文用到的是LitePal,不懂的可以查看我那篇關於LitePal的詳解

接下來是代碼時間

 1 //登錄頁面 actibity_main.xml
 2 <?xml version="1.0" encoding="utf-8"?>
 3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:orientation="vertical" android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6     <TextView
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:textColor="@color/colorPrimary"
10         android:textSize="25sp"
11         android:text="登錄界面"
12         android:layout_marginLeft="130sp"
13         android:layout_marginTop="150sp"
14         />
15     <LinearLayout
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:orientation="horizontal"
19         android:weightSum="1">
20         <TextView
21             android:text="用戶名:"
22             android:layout_width="wrap_content"
23             android:layout_height="wrap_content"
24             android:textSize="20sp"
25             android:layout_marginLeft="40sp"
26             />
27         <EditText
28             android:hint="請輸入用戶名"
29             android:id="@+id/editname"
30             android:background="@android:drawable/alert_light_frame"
31             android:layout_width="wrap_content"
32             android:layout_height="wrap_content"
33             android:layout_weight="0.61" />
34     </LinearLayout>
35     <LinearLayout
36         android:layout_width="match_parent"
37         android:layout_height="wrap_content"
38         android:orientation="horizontal"
39         android:weightSum="1">
40         <TextView
41             android:text="密碼:"
42             android:layout_width="wrap_content"
43             android:layout_height="wrap_content"
44             android:textSize="20sp"
45             android:layout_marginLeft="60sp"/>
46         <EditText
47             android:hint="請輸入密碼"
48             android:id="@+id/editpassword"
49             android:inputType="textPassword"
50             android:background="@android:drawable/alert_light_frame"
51             android:layout_width="wrap_content"
52             android:layout_height="wrap_content"
53             android:layout_weight="0.66" />
54     </LinearLayout>
55     <LinearLayout
56         android:orientation="horizontal"
57         android:layout_width="match_parent"
58         android:layout_height="wrap_content">
59         <Button
60             android:layout_width="wrap_content"
61             android:layout_height="wrap_content"
62             android:text="注冊"
63             android:id="@+id/register"
64             android:textSize="20sp"
65             android:layout_marginLeft="100sp"/>
66         <Button
67             android:layout_width="wrap_content"
68             android:layout_height="wrap_content"
69             android:text="登錄"
70             android:id="@+id/login"
71             android:layout_marginLeft="40sp"
72             android:textSize="20sp"/>
73     </LinearLayout>
74 </LinearLayout>
 1 //注冊頁面 login.xml
 2 <?xml version="1.0" encoding="utf-8"?>
 3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:orientation="vertical" android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6     <TextView
 7         android:layout_width="wrap_content"
 8         android:layout_height="wrap_content"
 9         android:textColor="@color/colorPrimary"
10         android:textSize="25sp"
11         android:text="創建新用戶"
12         android:layout_marginLeft="130sp"
13         android:layout_marginTop="150sp"
14         />
15     <LinearLayout
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content"
18         android:orientation="horizontal"
19         android:weightSum="1">
20         <TextView
21             android:text="用戶名:"
22             android:layout_width="wrap_content"
23             android:layout_height="wrap_content"
24             android:textSize="20sp"
25             android:layout_marginLeft="40sp"
26             />
27         <EditText
28             android:hint="請輸入用戶名"
29             android:id="@+id/edit_name"
30             android:background="@android:drawable/alert_light_frame"
31             android:layout_width="wrap_content"
32             android:layout_height="wrap_content"
33             android:layout_weight="0.61" />
34     </LinearLayout>
35     <LinearLayout
36         android:layout_width="match_parent"
37         android:layout_height="wrap_content"
38         android:orientation="horizontal"
39         android:weightSum="1">
40         <TextView
41             android:text="密碼:"
42             android:layout_width="wrap_content"
43             android:layout_height="wrap_content"
44             android:textSize="20sp"
45             android:layout_marginLeft="60sp"/>
46         <EditText
47             android:id="@+id/edit_password"
48             android:hint="請輸入密碼"
49             android:background="@android:drawable/alert_light_frame"
50             android:layout_width="wrap_content"
51             android:layout_height="wrap_content"
52             android:layout_weight="0.66" />
53     </LinearLayout>
54 
55     <LinearLayout
56         android:orientation="horizontal"
57         android:layout_width="match_parent"
58         android:layout_height="wrap_content">
59         <Button
60             android:layout_width="wrap_content"
61             android:layout_height="wrap_content"
62             android:text="創建新用戶"
63             android:id="@+id/creat"
64             android:textSize="20sp"
65             android:layout_marginLeft="90sp"/>
66         <Button
67             android:layout_width="wrap_content"
68             android:layout_height="wrap_content"
69             android:text="返回"
70             android:id="@+id/turnbefore"
71             android:layout_marginLeft="20sp"
72             android:textSize="20sp"/>
73     </LinearLayout>
74 </LinearLayout>
 1 //登錄成功的主頁面 jump.xml
 2 <?xml version="1.0" encoding="utf-8"?>
 3 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 4     android:orientation="vertical" android:layout_width="match_parent"
 5     android:layout_height="match_parent">
 6 <TextView
 7     android:layout_width="wrap_content"
 8     android:layout_height="wrap_content"
 9     android:text="成功登錄主頁面"
10     android:textColor="@color/colorPrimaryDark"
11     android:layout_marginLeft="80sp"
12     android:layout_marginTop="160sp"
13     android:textSize="30sp"/>
14 </LinearLayout>
 1 //Person類,用於用戶名和密碼的存儲
 2 package cct.login;
 3 import org.litepal.crud.DataSupport;
 4 public class Person extends DataSupport{
 5     private String useName;
 6     private String password;
 7     public String getUseName() {
 8         return useName;
 9     }
10     public void setUseName(String useName) {
11         this.useName = useName;
12     }
13     public String getPassword() {
14         return password;
15     }
16     public void setPassword(String password) {
17         this.password = password;
18     }
19 }
 1 //注冊界面的活動,LogActivity
 2 package cct.login;
 3 import android.content.Intent;
 4 import android.os.Bundle;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.view.View;
 7 import android.widget.Button;
 8 import android.widget.EditText;
 9 import android.widget.Toast;
10 import org.litepal.tablemanager.Connector;
11 public class LogActivity extends AppCompatActivity implements View.OnClickListener{
12     private EditText edit_name;
13     private EditText edit_password;
14     private Button creat;
15     private Button turnbefore;
16     @Override
17     protected void onCreate(Bundle savedInstanceState) {
18         super.onCreate(savedInstanceState);
19         setContentView(R.layout.login);
20         edit_name= (EditText) findViewById(R.id.edit_name);
21         edit_password= (EditText) findViewById(R.id.edit_password);
22         creat= (Button) findViewById(R.id.creat);
23         turnbefore= (Button) findViewById(R.id.turnbefore);
24         creat.setOnClickListener(this);
25         turnbefore.setOnClickListener(this);
26     }
27     @Override
28     public void onClick(View v) {
29         Person ps;
30         switch (v.getId()){
31             case R.id.creat:
32                 Connector.getDatabase();
33                 ps=new Person();
34                 String strname=edit_name.getText().toString();
35                 String strpassword=edit_password.getText().toString();
36                 ps.setUseName(strname);
37                 ps.setPassword(strpassword);
38                 ps.save();
39                 Toast.makeText(LogActivity.this,"創建新用戶成功",Toast.LENGTH_SHORT).show();
40                 break;
41             case R.id.turnbefore:
42                 Intent it=new Intent(LogActivity.this,MainActivity.class);
43                 startActivity(it);
44                 break;
45         }
46     }
47 }
 1 //重頭戲,登錄頁面的活動 MainActivity
 2 //注釋掉的部分是想的太復雜了,把數據庫里面的數據拿去封裝成map,
 3 //又封裝成list,然后又拆解成map,再拆成原數據,再與輸入的數據匹配
 4 //不過通過這個我學會怎么取list中的map中的數據,所以我沒有把原來的
 5 //代碼刪掉,而是注釋掉,給大家看看對比
 6 package cct.login;
 7 import android.content.Intent;
 8 import android.support.v7.app.AppCompatActivity;
 9 import android.os.Bundle;
10 import android.view.View;
11 import android.widget.Button;
12 import android.widget.EditText;
13 import android.widget.Toast;
14 import org.litepal.crud.DataSupport;
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.Iterator;
18 import java.util.List;
19 import java.util.Map;
20 
21 public class MainActivity extends AppCompatActivity implements View.OnClickListener{
22     private EditText editname;
23     private EditText editpassword;
24     private Button register;
25     private Button login;
26     List<Map<String, String>> item;
27     @Override
28     protected void onCreate(Bundle savedInstanceState) {
29         super.onCreate(savedInstanceState);
30         setContentView(R.layout.activity_main);
31         editname= (EditText) findViewById(R.id.editname);
32         editpassword= (EditText) findViewById(R.id.editpassword);
33         register= (Button) findViewById(R.id.register);
34         login= (Button) findViewById(R.id.login);
35         register.setOnClickListener(this);
36         login.setOnClickListener(this);
37     }
38     @Override
39     public void onClick(View v) {
40         switch (v.getId()) {
41             case R.id.register:
42                 Intent it = new Intent(MainActivity.this, LogActivity.class);
43                 startActivity(it);
44                 break;
45             case R.id.login:
46                 //設置成功匹配的標志,假設為false,即假設不匹配
47                 boolean flag=false;
48                 //用來保存遍歷中的用戶名
49                // String str1 = null;
50                 //用來保存遍歷中的密碼
51                // String str2 = null;
52                 //申請有個用來存放(用戶和密碼)這個整體的list
53                 //item = new ArrayList<Map<String, String>>();
54                 //LitePal里面遍歷查詢所有數據的方法
55                 List<Person> persons = DataSupport.findAll(Person.class);
56                 //把數據庫里面的數據取出,存在list中
57                 for (Person ps : persons) {
58 //                    Map<String, String> map = new HashMap<String, String>();
59 //                    //綁定用戶名和密碼
60 //                    map.put(ps.getUseName(),ps.getPassword());
61 //                    //將其看作一個整體,放入list中
62 //                    item.add(map);
63 //                }
64 //                //遍歷每一項的list,得到其中的Map對象
65 //                    for (int i = 0; i < item.size(); i++) {
66 //                        Map<String, String> m = (Map<String, String>) item.get(i);
67 //                        //使用迭代法來取Map中的鍵(用戶名)和值(密碼),相當於解封裝,因為要分別驗證是否匹配
68 //                        Iterator<String> iterator = m.keySet().iterator();
69 //                        while (iterator.hasNext()) {
70 //                            str1 = iterator.next();
71 //                            str2 = m.get(str1);
72                             //檢測是否匹配
73                             if(editname.getText().toString().trim().equals(ps.getUseName()) &&
74                                     (editpassword.getText().toString().trim().equals(ps.getPassword())))
75                             {
76                                 //匹配就設true
77                                 flag=true;
78                             }
79                         }
80                 if (flag)
81             {
82                 //如果匹配才進入主頁面
83                 Intent s = new Intent(MainActivity.this, JumpActivity.class);
84                 startActivity(s);
85             }else{
86                     Toast.makeText(MainActivity.this,"登錄失敗",Toast.LENGTH_SHORT).show();
87                 }
88                     break;
89                 }
90         }
91     }
 1 //最后是主頁面活動,僅僅用於跳轉  JUmpActivity
 2 package cct.login;
 3 import android.os.Bundle;
 4 import android.support.v7.app.AppCompatActivity;
 5 public class JumpActivity extends AppCompatActivity {
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.jump);
10     }
11 }

最后不要忘記在AndroidManifest.xml文件的里添加

1             <application>
2                 ......
3              <activity android:name=".LogActivity"></activity>
4              <activity android:name=".JumpActivity"></activity>
5             </application>

以上是全部內容,若有錯誤的地方,歡迎指正。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM