網(wǎng)站首頁 編程語言 正文
?
//注冊功能 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
相關(guān)推薦
- 2024-02-16 SpringBoot 事務(wù)的屬性rollbackFor 與 propagetion
- 2022-12-15 Native?Memory?Tracking追蹤區(qū)域示例分析_React
- 2022-01-10 Layui碰上Thymeleaf的解析問題
- 2022-12-05 C++?Boost?MultiArray簡化使用多維數(shù)組庫_C 語言
- 2023-04-08 C#獲取時間戳的方法及時間戳轉(zhuǎn)換問題_C#教程
- 2023-01-09 Redis排序命令Sort深入解析_Redis
- 2022-07-07 python中列表對象pop()方法的使用說明_python
- 2022-10-04 python中numpy矩陣的零填充的示例代碼_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支