Android實(shí)現(xiàn)登錄注冊頁面(下)
前面我們已經(jīng)完成了登錄注冊頁面的布局,下面我們實(shí)現(xiàn)驗(yàn)證登錄和記住密碼的功能。
我們這里還沒用到數(shù)據(jù)庫,所以我們的驗(yàn)證的賬號密碼,是寫死的。
首先進(jìn)入登錄頁面,可以從這里跳轉(zhuǎn)到注冊頁面,注冊成功后,賬號密碼的輸入框會(huì)自動(dòng)獲取剛剛注冊的賬號密碼,無需再次輸入。登錄成功后,頁面跳轉(zhuǎn)到首頁,首頁獲取并顯示剛剛注冊的賬號,點(diǎn)擊首頁的退出登錄,則返回到登錄頁面。



接下來,先寫首頁activity_main.xml頁面的內(nèi)容:
<?xml version="1.0" encoding="utf-8"?> <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" ? ? android:orientation="vertical" ? ? tools:context=".MainActivity"> ? ? ? <TextView ? ? ? ? android:id="@+id/tv_content" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="歡迎!" ? ? ? ? android:textSize="40sp" ? ? ? ? android:layout_marginTop="30dp" ? ? ? ? android:layout_gravity="center_horizontal"></TextView> ? ? <Button ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:text="退出登錄" ? ? ? ? android:textSize="25sp" ? ? ? ? android:layout_margin="20dp" ? ? ? ? style="@style/MyBtnStyle" ? ? ? ? android:onClick="loginOut"></Button> </LinearLayout>
效果如圖:

首頁的MainActivity.java代碼如下:
public class MainActivity extends AppCompatActivity {
?
? ? private TextView tvContent;
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? getSupportActionBar().setTitle("首頁");
?
? ? ? ? tvContent=findViewById(R.id.tv_content);
? ? ? ? Intent intent=getIntent();
? ? ? ? String account=intent.getStringExtra("account");
? ? ? ? tvContent.setText("歡迎你:"+account);
? ? }
?
? ? //退出登錄按鈕點(diǎn)擊事件
? ? public void loginOut(View view) {
? ? ? ? Intent intent=new Intent(this,LoginActivity.class);
? ? ? ? startActivity(intent);
? ? ? ? this.finish();
? ? }
}這里的代碼是包含驗(yàn)證登錄的內(nèi)容和記住密碼的內(nèi)容。
首先是LoginActivity.java 頁面:
public class LoginActivity extends AppCompatActivity {
?
? ? public static final int REQUEST_CODE_REGISTER = 1;
? ? private static final String TAG="tag";
? ? private Button btnLogin;
? ? private EditText etAccount,etPassword;
? ? private CheckBox cbRemember;
? ? private String userName="a";
? ? private String pass="123";
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_login);
? ? ? ? getSupportActionBar().setTitle("登錄");
?
? ? ? ? initView();
? ? ? ? initData();
?
?
? ? ? ? btnLogin.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? //判斷賬號密碼是否符合要求
? ? ? ? ? ? ? ? String account=etAccount.getText().toString();
? ? ? ? ? ? ? ? String password=etPassword.getText().toString();
? ? ? ? ? ? ? ? if (TextUtils.isEmpty(userName)){
? ? ? ? ? ? ? ? ? ? Toast.makeText(LoginActivity.this,"還沒有注冊賬號!",Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? if (TextUtils.equals(account,userName)){
? ? ? ? ? ? ? ? ? ? if (TextUtils.equals(password,pass)){
? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(LoginActivity.this,"恭喜你,登錄成功!",Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ? ? ? ? ? if (cbRemember.isChecked()){
? ? ? ? ? ? ? ? ? ? ? ? ? ? SharedPreferences spf=getSharedPreferences("spfRecorid",MODE_PRIVATE);
? ? ? ? ? ? ? ? ? ? ? ? ? ? SharedPreferences.Editor edit=spf.edit();
? ? ? ? ? ? ? ? ? ? ? ? ? ? edit.putString("account",account);
? ? ? ? ? ? ? ? ? ? ? ? ? ? edit.putString("password",password);
? ? ? ? ? ? ? ? ? ? ? ? ? ? edit.putBoolean("isRemember",true);
? ? ? ? ? ? ? ? ? ? ? ? ? ? edit.apply();
? ? ? ? ? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? ? ? ? ? SharedPreferences spf=getSharedPreferences("spfRecorid",MODE_PRIVATE);
? ? ? ? ? ? ? ? ? ? ? ? ? ? SharedPreferences.Editor edit=spf.edit();
? ? ? ? ? ? ? ? ? ? ? ? ? ? edit.putBoolean("isRemember",false);
? ? ? ? ? ? ? ? ? ? ? ? ? ? edit.apply();
? ? ? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? ? ? ? ? //實(shí)現(xiàn)跳轉(zhuǎn)
? ? ? ? ? ? ? ? ? ? ? ? Intent intent=new Intent(LoginActivity.this,MainActivity.class);
? ? ? ? ? ? ? ? ? ? ? ? //傳遞用戶名
? ? ? ? ? ? ? ? ? ? ? ? intent.putExtra("account",account);
? ? ? ? ? ? ? ? ? ? ? ? startActivity(intent);
? ? ? ? ? ? ? ? ? ? ? ? //接收自己
? ? ? ? ? ? ? ? ? ? ? ? LoginActivity.this.finish();
?
? ? ? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(LoginActivity.this,"密碼錯(cuò)誤!",Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? Toast.makeText(LoginActivity.this,"用戶名錯(cuò)誤",Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? }
? ? ? ? });
? ? }
?
? ? //記住密碼(取出數(shù)據(jù))
? ? private void initData() {
? ? ? ? SharedPreferences spf=getSharedPreferences("spfRecorid",MODE_PRIVATE);
? ? ? ? boolean isRemember=spf.getBoolean("isRemember",false);
? ? ? ? String account=spf.getString("account","");
? ? ? ? String password=spf.getString("password","");
? ? ? ? //更新用戶名密碼(注冊的用戶名密碼)
? ? ? ? userName=account;
? ? ? ? pass=password;
?
? ? ? ? if (isRemember){
? ? ? ? ? ? etAccount.setText(account);
? ? ? ? ? ? etPassword.setText(password);
? ? ? ? ? ? cbRemember.setChecked(true);
? ? ? ? }
?
? ? }
?
? ? //初始化
? ? private void initView(){
? ? ? ? btnLogin=findViewById(R.id.btn_login);
? ? ? ? etAccount=findViewById(R.id.et_account);
? ? ? ? etPassword=findViewById(R.id.et_password);
? ? ? ? cbRemember=findViewById(R.id.cb_remember);
? ? }
?
? ? //還沒有賬號(跳轉(zhuǎn)到注冊)
? ? public void toRegister(View view) {
? ? ? ? Intent intent=new Intent(this,RegisterActivity.class);
? ? ? ??
? ? ? ? //startActivity(intent);
? ? ? ? startActivityForResult(intent,REQUEST_CODE_REGISTER);
? ? }
?
? ? @Override
? ? protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
? ? ? ? super.onActivityResult(requestCode, resultCode, data);
? ? ? ? if (requestCode==REQUEST_CODE_REGISTER&&resultCode==RegisterActivity.RESULT_CODE_REGISTER&&data!=null){
? ? ? ? ? ? Bundle extras=data.getExtras();
? ? ? ? ? ? //獲取用戶密碼
? ? ? ? ? ? String account=extras.getString("account","");
? ? ? ? ? ? String password=extras.getString("password","");
? ? ? ? ? ? etAccount.setText(account);
? ? ? ? ? ? etPassword.setText(password);
?
? ? ? ? ? ? //更新用戶名密碼(注冊的用戶名密碼)
? ? ? ? ? ? userName=account;
? ? ? ? ? ? pass=password;
? ? ? ? }
? ? }
}接下來是RegisterActivity.java 頁面的內(nèi)容
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
?
? ? public static final int RESULT_CODE_REGISTER = 0;
? ? private Button btnRegister;
? ? private EditText etAccount,etPass,etPassConfirm;
? ? private CheckBox cbAgree;
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_register);
? ? ? ? getSupportActionBar().setTitle("注冊");
?
? ? ? ? etAccount=findViewById(R.id.et_account);
? ? ? ? etPass=findViewById(R.id.et_password);
? ? ? ? etPassConfirm=findViewById(R.id.et_password_Confirm);
? ? ? ? cbAgree=findViewById(R.id.cb_agree);
? ? ? ? btnRegister=findViewById(R.id.btn_register);
?
? ? ? ? btnRegister.setOnClickListener(this);
? ? }
?
? ? @Override
? ? public void onClick(View v) {
? ? ? ? String name=etAccount.getText().toString();
? ? ? ? String pass=etPass.getText().toString();
? ? ? ? String PassConfirm=etPassConfirm.getText().toString();
?
? ? ? ? if (TextUtils.isEmpty(name)){
? ? ? ? ? ? Toast.makeText(RegisterActivity.this,"用戶名不能為空!",Toast.LENGTH_LONG).show();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? if (TextUtils.isEmpty(pass)){
? ? ? ? ? ? Toast.makeText(RegisterActivity.this,"密碼不能為空!",Toast.LENGTH_LONG).show();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? if (!TextUtils.equals(pass,PassConfirm)){
? ? ? ? ? ? Toast.makeText(RegisterActivity.this,"密碼不一致!",Toast.LENGTH_LONG).show();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? if (!cbAgree.isChecked()){
? ? ? ? ? ? Toast.makeText(RegisterActivity.this,"請同意用戶協(xié)議!",Toast.LENGTH_LONG).show();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? //存儲(chǔ)注冊的用戶名和密碼
? ? ? ? SharedPreferences spf=getSharedPreferences("spfRecorid",MODE_PRIVATE);
? ? ? ? SharedPreferences.Editor edit = spf.edit();
? ? ? ? edit.putString("account",name);
? ? ? ? edit.putString("password",pass);
? ? ? ? //注冊成功后,回到登錄頁面,數(shù)據(jù)回傳
? ? ? ? Intent intent=new Intent();
? ? ? ? Bundle bundle=new Bundle();
? ? ? ? bundle.putString("account",name);
? ? ? ? bundle.putString("password",pass);
? ? ? ? intent.putExtras(bundle);
? ? ? ? setResult(RESULT_CODE_REGISTER,intent);
? ? ? ? Toast.makeText(RegisterActivity.this,"注冊成功!",Toast.LENGTH_LONG).show();
? ? ? ? this.finish();
? ? }
}到這里,驗(yàn)證登錄和記住密碼的功能就完成啦!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android用SharedPreferences實(shí)現(xiàn)登錄注冊注銷功能
- android實(shí)現(xiàn)注冊登錄程序
- Android實(shí)現(xiàn)登錄注冊功能
- Android基于Sqlite實(shí)現(xiàn)注冊和登錄功能
- Android實(shí)現(xiàn)注冊登錄界面的實(shí)例代碼
- Android設(shè)計(jì)登錄界面、找回密碼、注冊功能
- Android客戶端實(shí)現(xiàn)注冊、登錄詳解(1)
- Android登錄注冊功能 數(shù)據(jù)庫SQLite驗(yàn)證
- Android Studio連接SQLite數(shù)據(jù)庫的登錄注冊實(shí)現(xiàn)
- Android使用http實(shí)現(xiàn)注冊登錄功能
相關(guān)文章
Android shell命令行中過濾adb logcat輸出的方法
本文主要介紹Android shell命令行中過濾adb logcat輸出,這里詳細(xì)說明了shell 命令過濾logcat 輸出內(nèi)容,有需要的小伙伴可以參考下2016-08-08
Android實(shí)現(xiàn)網(wǎng)絡(luò)加載圖片點(diǎn)擊大圖后瀏覽可縮放
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)絡(luò)加載圖片點(diǎn)擊大圖后瀏覽可縮放,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Android中一個(gè)應(yīng)用實(shí)現(xiàn)多個(gè)圖標(biāo)的幾種方式
這篇文章主要給大家介紹了在Android中一個(gè)應(yīng)用如何實(shí)現(xiàn)多個(gè)圖標(biāo)的幾種方式,其中包括了多Activity + intent-filter方式、activity-alias方式以及網(wǎng)頁標(biāo)簽-添加快捷方式,分別給出了詳細(xì)的示例代碼,需要的朋友可以參考借鑒。2017-05-05
利用libmp3lame實(shí)現(xiàn)在Android上錄音MP3文件示例
本篇文章主要介紹了利用Lame庫實(shí)現(xiàn)在Android上錄音MP3文件示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03
Android實(shí)現(xiàn)隨機(jī)生成驗(yàn)證碼
在登錄注冊軟件時(shí),經(jīng)常會(huì)要求填寫隨機(jī)驗(yàn)證碼,這篇文章為大家詳細(xì)主要介紹了Android實(shí)現(xiàn)隨機(jī)生成驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
Gradle?Build?Cache引發(fā)的Task緩存編譯問題
這篇文章主要為大家介紹了Gradle?Build?Cache引發(fā)的Task緩存編譯問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
android實(shí)現(xiàn)手機(jī)截屏并保存截圖功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)手機(jī)截屏并保存截圖功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Android 實(shí)現(xiàn)沉浸式狀態(tài)欄的方法
沉浸式狀態(tài)欄的來源就是很多手機(jī)用的是實(shí)體按鍵,沒有虛擬鍵,于是開了沉浸模式就只有狀態(tài)欄消失了。下面腳本之家小編給大家介紹Android 實(shí)現(xiàn)沉浸式狀態(tài)欄,需要的朋友可以參考下2015-09-09
Android 中 requestWindowFeature()的應(yīng)用
本文主要介紹 Android requestWindowFeature()方法,這里對 requestWindowFeature()方法進(jìn)行詳解,對應(yīng)用程序窗體顯示狀態(tài)的操作有進(jìn)一步了解,希望能幫助有需要的小伙伴2016-07-07

