寫在前面
接前面一篇博客,前面介紹了app的大體功能季二,接下來將對這個(gè)Demo拆分成幾個(gè)部分來詳細(xì)介紹其中的布局檩咱、實(shí)現(xiàn)的原理、用到的技術(shù)等胯舷。對了刻蚯,這個(gè)Demo的服務(wù)器代碼也會(huì)在后面的文章中單獨(dú)的介紹。
效果圖
具體的實(shí)現(xiàn)
界面分析
具體功能
1.用兩個(gè)EditText接收用戶輸入的賬號密碼桑嘶;
2.一個(gè)按鈕接收用戶登錄事件炊汹;
3.用TextView來提示“忘記密碼”和“注冊賬號”入口;
4.可以查看用戶服務(wù)條款逃顶。
賬號密碼輸入框:使用自定義的樣式改變光標(biāo)顏色讨便、下劃線顏色、選中文本的背景顏色等以政。
<style name="LoginEditTextTheme" parent="AccountTextStyle">
<item name="android:textColorHint">@color/alpha70_white</item>
<!--光標(biāo)顏色-->
<item name="android:textCursorDrawable">@drawable/cursor</item>
<item name="android:theme">@style/Theme.Custom.Control.EditText</item>
<!--下劃線顏色-->
<item name="android:backgroundTint">@color/transparent</item>
<!--選中文本的背景顏色-->
<item name="android:textColorHighlight">@color/alpha50_light_green</item>
<!--<item name="android:textSelectHandle">@color/alpha90_green</item>-->
<!--小水滴圖標(biāo)-->
<item name="android:textSelectHandleLeft">@drawable/custom_text_select_handle_left_material</item>
<item name="android:textColorHighlightInverse">@drawable/custom_text_select_handle_middle_material</item>
<item name="android:textSelectHandleRight">@drawable/custom_text_select_handle_right_material</item>
</style>
在輸入框后面使用一個(gè)CheckBox來實(shí)現(xiàn)下拉歷史賬號的功能霸褒。
<EditText
android:id="@+id/et_input_account"
style="@style/LoginEditTextTheme"
android:layout_width="wrap_content"
android:layout_height="@dimen/input_account_edit_text_height"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="10dp"
android:layout_toStartOf="@+id/iv_clear_account"
android:hint="@string/input_account"
android:inputType="number" />
<ImageView
android:id="@+id/iv_clear_account"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_toStartOf="@id/cb_login_drop_down"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:src="@mipmap/clear" />
<CheckBox
android:id="@+id/cb_login_drop_down"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:background="@drawable/account_drop_down"
android:button="@null"/>
忘記密碼:使用自定義樣式改變其顏色等
<style name="ClickableTextView">
<item name="android:textColor">@drawable/bg_hypertext</item>
<item name="android:clickable">true</item>
<item name="android:focusable">true</item>
</style>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_forget_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/login_forget_password"
style="@style/ClickableTextView" />
<TextView
android:id="@+id/tv_register_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_marginTop="20dp"
android:text="@string/login_register"
style="@style/ClickableTextView"/>
</RelativeLayout>
注冊功能的實(shí)現(xiàn)
1.接收用戶的個(gè)人信息。這里使用SharedPreferences存儲(chǔ)方式保存用戶信息
2.注冊時(shí)有兩次密碼的輸入盈蛮,并且進(jìn)行比較废菱;
3.注冊成功后采用彈窗的方式提示隨機(jī)生成的用戶賬號,并且將賬號保存到手機(jī)中抖誉。
SharedPreferences存儲(chǔ)
Android 中的 SharedPreference 是輕量級的數(shù)據(jù)存儲(chǔ)方式殊轴,能夠保存簡單的數(shù)據(jù)類型,比如 String寸五、int梳凛、boolean 值等耿币。其內(nèi)部是以 XML 結(jié)構(gòu)保存在 /data/data/包名/shared_prefs 文件夾下梳杏,數(shù)據(jù)以鍵值對的形式保存。下面就是這個(gè)demo中的數(shù)據(jù)存儲(chǔ)的例子:
(這里只是簡單的介紹!J浴)
// 創(chuàng)建SharedPreferences對象
SharedPreferences shp = context.getSharedPreferences("USER_DATA",Context.MODE_PRIVATE);
// 創(chuàng)建Editor對象寫入值
SharedPreferences.Editor editor = shp.edit();
// 寫入注冊時(shí)間
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日" + "hh:mm:ss");
editor.putString("register_time", sdf.format(new Date()));
注冊時(shí)用戶密碼比較與保存
private String register(EditText rUserPsd, EditText rUserAgainPad) {
String first_psa = rUserPsd.getText().toString();
String again_psa = rUserAgainPad.getText().toString();
// 判斷密碼是否一致
if (first_psa.equals(again_psa)){
// 兩次密碼一致
showExitDialog();
// Toast.makeText(Register.this,"注冊成功,賬號為:666666",Toast.LENGTH_SHORT).show();
// 跳轉(zhuǎn)
// inten();
return first_psa;
}else {
// 密碼不一致叛溢,重新輸入
Toast.makeText(Register.this,"兩次密碼不一致,重新輸入",Toast.LENGTH_SHORT).show();
rUserAgainPad.setText("");
rUserAddress.setText("");
}
return null;
}
用戶信息寫入
public class SaveRegisterData {
public EditText UserName; // 昵稱
public EditText UserPsd; // 密碼
public int UserGender; // 性別
public EditText UserAddress; // 地址
private Context context;
public SaveRegisterData(Context context){
this.context = context;
}
public void save(EditText inUserName, String inUserPsd, int inGunder, EditText inUserAddress,String inUserNumber){
String saveName = inUserName.getText().toString();//接收用戶名EditText內(nèi)容
String savepsd = inUserPsd;//接收密碼EditText內(nèi)容
//String againpsd = inAgainPsd.getText().toString();//接收密碼EditText內(nèi)容
String saveAddress = inUserAddress.getText().toString();//接收地址EditText內(nèi)容
// 創(chuàng)建SharedPreferences對象
SharedPreferences shp = context.getSharedPreferences("USER_DATA",Context.MODE_PRIVATE);
// 創(chuàng)建Editor對象寫入值
SharedPreferences.Editor editor = shp.edit();
// 寫入注冊時(shí)間
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日" + "hh:mm:ss");
editor.putString("register_time", sdf.format(new Date()));
// 寫入用戶信息
editor.putString("UserName", saveName);
editor.putString("UserPsd", savepsd);
if (inGunder==0){
editor.putString("UserGender","男");
}else if (inGunder == 1){
editor.putString("UserGender","女");
}else {
editor.putString("UserGender","未知");
}
editor.putString("UserAddress", saveAddress);
editor.putString("UserNumber",inUserNumber);
editor.commit();
}
// public String outData(){
// // 創(chuàng)建SharedPreferences對象
// SharedPreferences shp_out = context.getSharedPreferences("USER_DATA",Context.MODE_PRIVATE);
// // 創(chuàng)建Editor對象寫入值
// SharedPreferences.Editor editor = shp_out.edit();
// return shp_out.getString("UserNumber","");
// }
}
隨機(jī)生成賬號
這里只隨機(jī)生成六位數(shù)的賬號
// 生成隨機(jī)賬號
public String random(){
String strRand="" ;
for(int i=0;i<6;i++){
strRand += String.valueOf((int)(Math.random() * 10)) ;
}
return strRand;
}
提示用戶的賬號
// 生成賬號提示框
public void showExitDialog(){
new AlertDialog.Builder(this)
.setTitle("注冊成功")
.setMessage("賬號:"+UserNumber)
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
inten();
}
})
.show();
}
找回密碼功能實(shí)現(xiàn)
由于能力有限,通過郵箱等方式找回密碼后期再來補(bǔ)充劲适,這里提供的是:用戶輸入昵稱和賬號找回楷掉;
原理:提取出存儲(chǔ)在手機(jī)中的昵稱和賬號,如果兩者都匹配霞势,則返回密碼
private void findpsd() {
// 獲取輸入
String number = fUserNumber.getText().toString();
String phoneNumber = fUserPhoneNumber.getText().toString();
UpRegisterData upRegisterData = new UpRegisterData(getApplicationContext());
String user_num = upRegisterData.Up_user_number();
String user_name = upRegisterData.Up_user_name();
user_passwprd = upRegisterData.Up_user_password();
// System.out.println(name);
// System.out.println(pass);
if (user_num.equals(number) && user_name.equals(phoneNumber)){
// 提示成功
//Toast.makeText(FindPsd.this,"已將相關(guān)信息發(fā)送至郵箱",Toast.LENGTH_SHORT).show();
showExitDialog();
}else if (number==null || phoneNumber==null){
Toast.makeText(FindPsd.this,"請輸入賬號與綁定手機(jī)號",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(FindPsd.this,"輸入信息有誤烹植,請重新輸入",Toast.LENGTH_SHORT).show();
// 清空輸入框的內(nèi)容
fUserNumber.setText("");
fUserPhoneNumber.setText("");
}
}