Android实现简单的登录和注册功能

GitHub地址:https://github.com/PeachLuis/LoginTest

登录和注册功能app

一.UI设计

这里我想要仿照现在的qq登录界面,实现不止隐藏ActionBar,还要隐藏顶端状态栏,实现全屏幕的沉浸式体验

1.实现隐藏状态栏和全屏幕

在res-values-styles.xml中添加一个我们自制的theme主题

  <style name="FirstTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowFullscreen">true</item>
    </style>

然后在AndroidManifest.xml中在我们需要的activity下面添加我们自制的这个theme

<activity
            android:name=".MainActivity"
            android:label="FirstActivity"
            android:theme="@style/FirstTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

以上的这种方法是我看见的容易实现的一种,还有一种方式需要一些条件,在我的手机上无法运行,可以参考一下

2.图片设置为背景被拉伸的解决方案

我刚开始直接将这个图片设置为src,但是发现并不能填满屏幕;我又将图片设置为background,但是很遗憾图片被拉伸变形了,虽然在隐藏状态栏加全屏显示后图片能完整的填满屏幕,但是针对另外一些情况,可以将图片适当放大,即使用ImageView控件的scaleType属性。详情见

3.设置圆角Button按钮

在res右键创建新的Android Resource File,设置name为btn_selector,element选择为selector,设置按下和不按下的shape样式

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/btn_shape_normal" android:state_pressed="false" />
    <item android:drawable="@drawable/btn_shape_pressed" android:state_pressed="true" />

</selector>

然后新建btn_shape_normal和btn_shape_pressed

<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners
        android:radius="10dp" />
    <size
        android:height="45dp"
        android:width="45dp" />
    <stroke
        android:color="#fff"
        android:width="1dp" />

</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners
        android:radius="10dp" />
    <size
        android:height="45dp"
        android:width="45dp" />
    <stroke
        android:color="#fff"
        android:width="1dp" />
    <solid
        android:color="#b1ccda" />

</shape>

详细属性设置如下:

img

4.设置圆形图片

在网上看到的一种自制CircleImageView的代码实现

二.运用数据库功能

1.实现退出登录活动后再进入可以记住账号

可以采用SharedPreferences储存方法,编写一个save方法和一个read方法,再在代码中进行调用。

但是考虑到活动的生命期,应该是将save方法放在登录按钮的点击事件当中,而read方法放在重写的onResume()当中。

save方法:

private void save() {
        SharedPreferences.Editor editor = getSharedPreferences("account_password",MODE_PRIVATE).edit();
        editor.putString("account", account.getText().toString());
        editor.putString("password", password.getText().toString());
        editor.apply();
    }

read方法:

private void read() {
        SharedPreferences pref = getSharedPreferences("account_password", MODE_PRIVATE);
        account.setText(pref.getString("account", ""));
        password.setText(pref.getString("password",""));
    }

关于SharePreferences的更多知识请看这里:

2.实现记住密码

这里就在xml文件中添加复选框CheckBox,

 <LinearLayout
        android:id="@+id/linear_1"
        android:layout_below="@id/password"
        android:layout_alignLeft="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <CheckBox
            android:id="@+id/remember_pass"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18dp"
            android:text="记住密码"/>
    </LinearLayout>

然后在save和read方法中添加逻辑

public class LoginActivity extends AppCompatActivity {
    private EditText account,password;
    private CheckBox rememberPassword;
    ...
        
    private void save() {
        SharedPreferences.Editor editor = getSharedPreferences("account_password",MODE_PRIVATE).edit();
        editor.putString("account", account.getText().toString());
        editor.putString("password", password.getText().toString());
        editor.putBoolean("remember_password", rememberPassword.isChecked());
        editor.apply();
    }
    
    private void read() {
        SharedPreferences pref = getSharedPreferences("account_password", MODE_PRIVATE);
        account.setText(pref.getString("account", ""));
        if (pref.getBoolean("remember_password",false)) {
           password.setText(pref.getString("password",""));
        }
    }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章