• 登录页面(动态地与数据库匹配用户信息)


    初衷:写这个功能主要是发现很多人写注册登录都把账号密码写死,不能动态更新数据,让我很不喜欢,根本满足不了需求,

    而且第一次安装的人,如果不知道账号和密码,就无法登录,所以我觉得写一个可以注册用户并登录的应用势在必行。

    优点: 这是一个比较完善的通过注册用户名和密码,实时地更新数据库中用户名和密码信息,然后你在登录页面,输入用户名和密码

    ,查询是否在数据库中能找到已有的信息与之匹配,若匹配,则可以登录主页面,若不能匹配则说明不存在该用户,提示登录失败。

    本博文用到的是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>

    以上是全部内容,若有错误的地方,欢迎指正。

  • 相关阅读:
    js 简单getByClass得封装
    微信红包的随机算法
    js 淘宝评分
    HDU 1023 Train Problem II( 大数卡特兰 )
    HDU 1576 A/B( 逆元水 )
    HDU 5533 Dancing Stars on Me( 有趣的计算几何 )
    POJ 1664 放苹果( 递推关系 )
    HDU 2095 find your present (2)( 位运算 )
    POJ 3517 And Then There Was One( 约瑟夫环模板 )
    POJ 1988 Cube Stacking( 带权并查集 )*
  • 原文地址:https://www.cnblogs.com/cct1314520/p/6516648.html
Copyright © 2020-2023  润新知