最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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);
    }
 
    //設(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("確認", 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="請確認密碼"
        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("用戶名或密碼輸入錯誤!");
        }
    }
 
    //設(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("確認", 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="確認登錄"
        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?利用共享存儲進行用戶的注冊和登錄驗證的文章就介紹到這了,更多相關(guān)Android?studio?注冊和登錄驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

清苑县| 嘉峪关市| 鄄城县| 巫溪县| 马边| 青河县| 揭东县| 大石桥市| 遂平县| 凭祥市| 攀枝花市| 西乌珠穆沁旗| 北安市| 高台县| 长子县| 双城市| 同江市| 长子县| 内乡县| 赞皇县| 保德县| 平安县| 牙克石市| 贡山| 大姚县| 清丰县| 汽车| 上栗县| 沽源县| 汾西县| 沁源县| 萍乡市| 徐闻县| 河西区| 宁海县| 浙江省| 鄯善县| 盐池县| 丹阳市| 鄂伦春自治旗| 芦山县|