(3)登录界面——登录

本博文禁止随意转载
登录界面只需要从数据库中读取所有用户信息进行依次对比,有相同的则登录成功进入用户的主界面,吐槽一下,微信做的Ui是真的烂,可能这就是成年人的社交软件的标准吧(在这里插入图片描述

布局部分

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.example.wechat.MainActivity"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="100dp"
         />
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="61dp"
        app:srcCompat="@android:drawable/sym_def_app_icon" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textSize="18sp"
            android:text="用户名"
            />
        <EditText
            android:id="@+id/name"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_weight="1"
            android:hint="用户名/手机号"
            />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:orientation="horizontal">
        <TextView
            android:layout_width="90dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textSize="18sp"
            android:text="密码"
            />
        <EditText
            android:id="@+id/password"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:layout_gravity="center_vertical"
            android:inputType="textPassword"

            />
    </LinearLayout>

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="登录" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:weightSum="1"
        android:orientation="horizontal">
        <CheckBox
            android:id="@+id/remember_password"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="记住密码"
           />
        <CheckBox
            android:id="@+id/at_login"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:text="自动登录"
            />
    </LinearLayout>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="10dp"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="70dp"
        android:gravity="center"
        android:orientation="horizontal"
        android:weightSum="1">

        <Button
            android:id="@+id/button2"
            style="@android:style/Widget.Holo.Light.Button.Small"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.2"
            android:background="#FFFCFC"
            android:text="注册" />

    </LinearLayout>

</LinearLayout>

上面的imageView可以以后放logo的图片或者显示输入只用户名后显示对应用户的头像,这里暂时不做处理,下面两个LinearLayout也与注册部分的形式基本相同,这里需要讲的是控制按钮大小的部分,同样是包含在LinearLayout中,这次在LinearLayout中加入android:weightSum=“数字”,这样,改变button中layout_weight=中数字与之的比例就可以改变按钮的大小了,在本次操作中还发现可以直接在AS的design部分中对控件进行众多操控,比如说这里注册的按钮的颜色就是直接改的。
还再强调一下android:gravity和layout_gravity的区别android:gravity是用于设置View中内容相对于View组件的对齐方式,而android:layout_gravity用于设置View组件相对于Container(LinearLayout等)的对齐方式。
活动部分

下面有大部分的解释我是直接写在代码后的,另起几行写太麻烦,而且阅读起来效率不高,有知识点类型或者设计角度的内容会单独讲

public class MainActivity extends AppCompatActivity {
    private EditText editText1;//这两个用来接收输入框内容
    private EditText editText2;
    private CheckBox rememberPass;//这三个全部用来供记住密码使用
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //LitePal.deleteAll(User.class);这行代码的作用是清除对应数据库中的全部数据,调试的时候会用到
        editText1 = (EditText) findViewById(R.id.name);
        editText2 = (EditText) findViewById(R.id.password);

     pref= PreferenceManager.getDefaultSharedPreferences(this);//调用SharedPreference,但是好像这种书写形式目前不被推荐,在AS中被划横线,但是还能运行
     rememberPass=(CheckBox)findViewById(R.id.remember_password);//与相应控件的id关联起来
    boolean isRemember=pref.getBoolean("remember_password",false);//与键的状态相对应,false就表明默认时候是未被选中的
    if(isRemember){//当记住密码被选中后
        String account=pref.getString("account","");//读取此时候输入的两行内容
        String password=pref.getString("password","");
       editText1.setText(account);//此出将两行代表记录性的字符串与对应输入行相关联,并且因为有pref,已经包存数据了
        editText2.setText(password);
        rememberPass.setChecked(true);//上面的按键判断不具有记录性,而这里具有记录性
    }

SharedPreference在前面注册部分讲过,主要就是用来储存用户的一些默认偏好

 Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String inputText1 = editText1.getText().toString();//和前面的注册相同形式,不解释了
            String inputText2 = editText2.getText().toString();
            int i=0;
            List<User> users = LitePal.findAll(User.class);//创建list,下面遍历所有用户数据
            for (User user1 : users) {
                if (((inputText1.equals(user1.getName()))||(inputText1.equals(user1.getPhonenumber()))) && (inputText2.equals(user1.getPassword()))) {
                    String a = "登录成功!努力加载中......";
                    Toast.makeText(MainActivity.this, a, Toast.LENGTH_SHORT).show();
                    editor=pref.edit();//
                    if(rememberPass.isChecked()) {
                        editor.putBoolean("remember_password",true);//此时调取数据后知道之前选择了记住密码,你再次打开软件后会再将对应键变为点击状态
                        editor.putString("account",inputText1);//因为前面已经将数据出口与输入行相关联了,这里直接调取出来
                        editor.putString("password",inputText2);
                    }
                    else{
                        editor.clear();//未点击记住密码的时候
                    }
                    editor.apply();//将调取出来的数据显现
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    startActivity(intent);
                    break;
                } else {
                    int e=(users.size()-1);
                    if(i>=e) {//相当于检索完了所有用户后才能确定是用户输入的有问题
                        String b = "用户名或密码错误";
                        Toast.makeText(MainActivity.this, b, Toast.LENGTH_SHORT).show();
                    }
                }
               i=i+1;
            }

        }
    });

这里登录因为用户数量不多,就不考虑算法效率了,直接遍历,而且前面注册也说了还没有考虑重名等情况,暂时能用,但是进入到用户主界面后(四个碎片那部分),点手机左下角的退出,我觉得理想情况下是直接整个软件退出,而不是返回到上一个活动,这里整个运行链都没有考虑到活动的destroy。

    Button button2 = (Button) findViewById(R.id.button2);//打开注册按钮的配置,老套路了
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, Userregistration.class);
            startActivity(intent);

        }
    });
}}

总结:感觉登录还是做的相对简陋的,经不起深度的测bug,但是基本功能都能实现
优化部分:(1)当多个用户都在同一设备使用此软件的时候,点击用户名的输入框应该可以显示一个小的下滑框显示曾经在此处登录过的用户的记录,并且可以手动删除记录。
(2)像qq,微信那样,当用户有一次登录成功的过程之后,登录界面应该可以直接调取用户设置的个人头像显示在中上方或者输入框左边。
(3)还是Ui美化,做的挺丑的。
(4)最后软件基本成型后把各个活动的destroy补上,目前为了方便调试程序还是暂时不要。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值