Android?studio?利用共享存儲進行用戶的注冊和登錄驗證功能
更新時間:2021年12月15日 10:34:25 作者:wyj2001
這篇文章主要介紹了Android?studio?利用共享存儲進行用戶的注冊和登錄驗證功能,包括注冊頁面布局及登錄頁面功能,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
?



//注冊功能
public class MainActivity extends AppCompatActivity {
//聲明共享存儲(全局變量)
private SharedPreferences spf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//在打開頁面時初始化共享存儲對象spf "users"表名
spf=getSharedPreferences("users", Context.MODE_PRIVATE);
}
/**
* 注冊
* key : value
* @param view
*/
public void register(View view){
//獲取頁面視圖組件
EditText accountEt = findViewById(R.id.account);
EditText passwordEt = findViewById(R.id.password);
EditText repwdEt = findViewById(R.id.repwd);
//獲取用戶名和密碼
String account =accountEt.getText().toString();
String password =passwordEt.getText().toString();
String repwd=repwdEt.getText().toString();
//表單驗證
//判斷用戶名是否為空
if (account!=null && !"".equals(account)){
//用戶名不為空
//比較輸入的用戶名是否已經(jīng)被注冊存在
if (account.equals(spf.getString("account",""))){
//用戶名已存在
//Toast.makeText(MainActivity.this, "該用戶名已存在!", Toast.LENGTH_SHORT).show();
showDialog("該用戶名已經(jīng)存在");
return;//終止方法執(zhí)行
}
}else{
//用戶名為空
//Toast方法適用于不嚴重的提醒情況 content:上下文 text:提示的信息內(nèi)容
//Toast.makeText(MainActivity.this, "用戶姓名不能為空!", Toast.LENGTH_SHORT).show();
showDialog("用戶名不能為空!");
return;//終止方法執(zhí)行
}
//密碼驗證
//判斷密碼是否為空
if (password==null || "".equals(password)){
//判斷密碼不能為空
//Toast.makeText(MainActivity.this, "密碼不能為空!", Toast.LENGTH_SHORT).show();
showDialog("密碼不能為空");
return;
}
//驗證兩次密碼是否相同
if (!password.equals(repwd)){
//Toast.makeText(MainActivity.this, "兩次密碼不一致!", Toast.LENGTH_SHORT).show();
showDialog("兩次密碼不一致");
return;
}
//保存用戶名和密碼
SharedPreferences.Editor editor=spf.edit();
editor.putString("account",account);//賬號名
editor.putString("password",password);//密碼
editor.apply();//提交數(shù)據(jù)
Toast.makeText(MainActivity.this, "注冊成功!", Toast.LENGTH_SHORT).show();
//跳轉(zhuǎn)到登錄頁面
Intent intent=new Intent(MainActivity.this,LoginActivity.class);
startActivity(intent);
}
//設置提示框
public void showDialog(String msg){
//1、創(chuàng)建AlertDialog.Builder對象
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
//2、設置提示窗口相關信息
builder.setTitle("提示");
builder.setMessage(msg);//提示信息
builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setCancelable(false);//點擊空白區(qū)域不能被關掉 true能被關掉
builder.show();//顯示提示框
}
}
//注冊頁面布局
<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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="注冊"
android:gravity="center_horizontal"
android:textSize="50sp"/>
<EditText
android:id="@+id/account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="請輸入賬號名"
android:textSize="20sp"/>
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="請輸入密碼"
android:textSize="20sp"/>
<EditText
android:id="@+id/repwd"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="請確認密碼"
android:textSize="20sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="確認注冊"
android:textSize="30sp"
android:layout_marginTop="20dp"
android:onClick="register"/>
</LinearLayout>
//登錄頁面功能
public class LoginActivity extends AppCompatActivity {
//聲明共享存儲(全局變量)
private SharedPreferences spf;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//在打開頁面時初始化共享存儲對象spf "users"表名
spf=getSharedPreferences("users", Context.MODE_PRIVATE);
}
/**
* 登錄
* @param view
*/
public void login(View view){
//獲取頁面視圖組件
EditText accountEt=findViewById(R.id.account);
EditText passwordEt=findViewById(R.id.password);
//獲取用戶名
String account=accountEt.getText().toString();
String password=passwordEt.getText().toString();
//表單驗證
//判斷用戶名是否為空
if (account==null || "".equals(account)){
showDialog("用戶名不能為空!");
return;
}
//判斷密碼是否為空
if (password==null || "".equals(password)){
showDialog("密碼不能為空!");
return;
}
//驗證登錄,將用戶輸入的用戶名和密碼和共享存儲里面的內(nèi)容進行比對
if (account.equals(spf.getString("account",""))&&
password.equals(spf.getString("password",""))){
showDialog("登錄成功!");
//登錄成功后跳轉(zhuǎn)到首頁
Intent intent=new Intent(LoginActivity.this,HomeActivity.class);
//傳遞登錄成功的用戶名
intent.putExtra("account",account);
startActivity(intent);
}else{
showDialog("用戶名或密碼輸入錯誤!");
}
}
//設置提示框
public void showDialog(String msg){
//1、創(chuàng)建AlertDialog.Builder對象
AlertDialog.Builder builder=new AlertDialog.Builder(LoginActivity.this);
//2、設置提示窗口相關信息
builder.setTitle("提示");
builder.setMessage(msg);//提示信息
builder.setPositiveButton("確認", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.setCancelable(false);//點擊空白區(qū)域不能被關掉 true能被關掉
builder.show();//顯示提示框
}
}
//登錄頁面布局
<RelativeLayout 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="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity"
android:padding="20dp">
<TextView
android:id="@+id/register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登錄"
android:textSize="40sp"
android:gravity="center_horizontal"/>
<EditText
android:id="@+id/account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="請輸入賬號名"
android:layout_below="@id/register"
android:textSize="20sp"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="請輸入密碼"
android:textSize="20sp"
android:layout_below="@id/account"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="確認登錄"
android:textSize="30sp"
android:layout_marginTop="20dp"
android:layout_below="@id/password"
android:onClick="login"/>
</RelativeLayout>
//首頁顯示歡迎信息
public class HomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
//獲取意圖
Intent intent=getIntent();
String account=intent.getStringExtra("account");
//頁面上顯示傳遞的內(nèi)容
//設置歡迎信息
TextView tv=findViewById(R.id.welcomMessage);
tv.setText("歡迎"+account+"登錄本系統(tǒng)!");
}
}
//首頁布局
<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="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:id="@+id/welcomMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35dp"
android:gravity="center_horizontal"
android:textColor="#99CCFF"/>
</LinearLayout>
用戶注冊信息:

到此這篇關于Android?studio?利用共享存儲進行用戶的注冊和登錄驗證的文章就介紹到這了,更多相關Android?studio?注冊和登錄驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android RecyclerView 實現(xiàn)快速滾動的示例代碼
本篇文章主要介紹了Android RecyclerView 實現(xiàn)快速滾動的示例代碼,具有一定的參考價值,有興趣的可以了解一下2017-09-09
android studio3.0.1無法啟動Gradle守護進程的解決方法
這篇文章主要為大家詳細介紹了android studio3.0.1無法啟動Gradle守護進程的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-08-08
Android 源碼淺析RecyclerView ItemAnimator
這篇文章主要為大家介紹了Android 源碼淺析RecyclerView ItemAnimator,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
Android中RecyclerView實現(xiàn)多級折疊列表效果(二)
這篇文章主要給大家介紹了Android中RecyclerView實現(xiàn)多級折疊列表的相關資料,文中介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-05-05
Android網(wǎng)絡狀態(tài)實時監(jiān)聽實例代碼(二)
這篇文章主要介紹了Android網(wǎng)絡狀態(tài)實時監(jiān)聽實例代碼(2)的相關資料,需要的朋友可以參考下2016-03-03
Android設備獲取掃碼槍掃描的內(nèi)容與可能遇到的問題解決
這篇文章主要給大家介紹了關于Android設備獲取掃碼槍掃描內(nèi)容的方法,以及在開發(fā)中可能會遇到的問題的解決方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧。2017-11-11

