欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

?

//注冊功能
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);//點(diǎn)擊空白區(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);//點(diǎn)擊空白區(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>

用戶注冊信息:

到此這篇關(guān)于Android?studio?利用共享存儲進(jìn)行用戶的注冊和登錄驗證的文章就介紹到這了,更多相關(guān)Android?studio?注冊和登錄驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • cocos2d-2.0-x-2.0.3 交叉編譯到android報錯解決

    cocos2d-2.0-x-2.0.3 交叉編譯到android報錯解決

    我用的是cocos2d-2.0-x-2.0.3 之前弄了一天也沒成功 今天來了下載了最新的ndk8 更新了sdk 又重新是了一遍 居然成功了,不知道是工具的版本問題還是哪一步出錯誤了,在這里詳細(xì)的整理一下,感興趣的朋友可以了解下
    2013-01-01
  • Android 使用ViewPager自動滾動循環(huán)輪播效果

    Android 使用ViewPager自動滾動循環(huán)輪播效果

    本文主要給大家介紹viewpager自動播放,循環(huán)滾動的效果,對android viewpager滾動相關(guān)知識感興趣的朋友可以參考下本篇文章
    2015-11-11
  • Android?startActivityForResult的調(diào)用與封裝詳解

    Android?startActivityForResult的調(diào)用與封裝詳解

    startActivityForResult?可以說是我們常用的一種操作了,目前有哪些方式實(shí)現(xiàn)?startActivityForResult?的功能呢?本文就來和大家詳細(xì)聊聊
    2023-03-03
  • Android實(shí)現(xiàn)圓形圖片的兩種方式

    Android實(shí)現(xiàn)圓形圖片的兩種方式

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圓形圖片的兩種方式,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Android中ImageCropper矩形、圓形 裁剪框的實(shí)現(xiàn)方法

    Android中ImageCropper矩形、圓形 裁剪框的實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于Android中ImageCropper矩形、圓形 裁剪框的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧
    2018-07-07
  • Android Service開發(fā)應(yīng)用實(shí)例

    Android Service開發(fā)應(yīng)用實(shí)例

    Android的服務(wù)是開發(fā)Android應(yīng)用程序的重要組成部分。不同于活動Activity,服務(wù)是在后臺運(yùn)行,服務(wù)沒有接口,生命周期也與活動Activity非常不同。通過使用服務(wù)我們可以實(shí)現(xiàn)一些后臺操作,比如想從遠(yuǎn)程服務(wù)器加載一個網(wǎng)頁等,下面來看看詳細(xì)內(nèi)容,需要的朋友可以參考下
    2022-12-12
  • Android取消EditText自動獲取焦點(diǎn)默認(rèn)行為

    Android取消EditText自動獲取焦點(diǎn)默認(rèn)行為

    在項目中,一進(jìn)入一個頁面, EditText默認(rèn)就會自動獲取焦點(diǎn),很是郁悶,Android 如何讓EditText不自動獲取焦點(diǎn)?于是搜集整理一番,曬出來和大家分享,希望對你們有所幫助
    2012-12-12
  • Android UI之ImageView實(shí)現(xiàn)圖片旋轉(zhuǎn)和縮放

    Android UI之ImageView實(shí)現(xiàn)圖片旋轉(zhuǎn)和縮放

    這篇文章主要介紹了Android UI之ImageView實(shí)現(xiàn)圖片旋轉(zhuǎn)和縮放的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2015-09-09
  • 解決Android studio xml界面無法預(yù)覽問題

    解決Android studio xml界面無法預(yù)覽問題

    這篇文章主要介紹了解決Android studio xml界面無法預(yù)覽問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-03-03
  • Android編程自定義菜單實(shí)現(xiàn)方法詳解

    Android編程自定義菜單實(shí)現(xiàn)方法詳解

    這篇文章主要介紹了Android編程自定義菜單實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了Android自定義菜單的布局、動畫及功能相關(guān)實(shí)現(xiàn)技巧與注意事項,需要的朋友可以參考下
    2017-02-02

最新評論