日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Android?studio?利用共享存儲進(jìn)行用戶的注冊和登錄驗證功能_Android

作者:wyj2001 ? 更新時間: 2022-02-11 編程語言

?

//注冊功能
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方法適用于不嚴(yán)重的提醒情況 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);
    }
 
    //設(shè)置提示框
    public void showDialog(String msg){
        //1、創(chuàng)建AlertDialog.Builder對象
        AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
        //2、設(shè)置提示窗口相關(guān)信息
        builder.setTitle("提示");
        builder.setMessage(msg);//提示信息
        builder.setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
 
            }
        });
        builder.setCancelable(false);//點擊空白區(qū)域不能被關(guān)掉  true能被關(guān)掉
        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="請確認(rèn)密碼"
        android:textSize="20sp"/>
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="確認(rèn)注冊"
        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)容進(jìn)行比對
        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("用戶名或密碼輸入錯誤!");
        }
    }
 
    //設(shè)置提示框
    public void showDialog(String msg){
        //1、創(chuàng)建AlertDialog.Builder對象
        AlertDialog.Builder builder=new AlertDialog.Builder(LoginActivity.this);
        //2、設(shè)置提示窗口相關(guān)信息
        builder.setTitle("提示");
        builder.setMessage(msg);//提示信息
        builder.setPositiveButton("確認(rèn)", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
 
            }
        });
        builder.setCancelable(false);//點擊空白區(qū)域不能被關(guān)掉  true能被關(guān)掉
        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="確認(rèn)登錄"
        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)容
        //設(shè)置歡迎信息
        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>

用戶注冊信息:

原文鏈接:https://blog.csdn.net/weixin_45565469/article/details/121940645

欄目分類
最近更新