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>
詳細屬性設置如下:
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",""));
}
}