Android實現(xiàn)登錄注冊頁面(下)
前面我們已經(jīng)完成了登錄注冊頁面的布局,下面我們實現(xiàn)驗證登錄和記住密碼的功能。
我們這里還沒用到數(shù)據(jù)庫,所以我們的驗證的賬號密碼,是寫死的。
首先進入登錄頁面,可以從這里跳轉(zhuǎn)到注冊頁面,注冊成功后,賬號密碼的輸入框會自動獲取剛剛注冊的賬號密碼,無需再次輸入。登錄成功后,頁面跳轉(zhuǎ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);
? ? }
?
? ? //退出登錄按鈕點擊事件
? ? public void loginOut(View view) {
? ? ? ? Intent intent=new Intent(this,LoginActivity.class);
? ? ? ? startActivity(intent);
? ? ? ? this.finish();
? ? }
}這里的代碼是包含驗證登錄的內(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();
? ? ? ? ? ? ? ? ? ? ? ? }
?
? ? ? ? ? ? ? ? ? ? ? ? //實現(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,"密碼錯誤!",Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? Toast.makeText(LoginActivity.this,"用戶名錯誤",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;
? ? ? ? }
? ? ? ? //存儲注冊的用戶名和密碼
? ? ? ? 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();
? ? }
}到這里,驗證登錄和記住密碼的功能就完成啦!
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android shell命令行中過濾adb logcat輸出的方法
本文主要介紹Android shell命令行中過濾adb logcat輸出,這里詳細說明了shell 命令過濾logcat 輸出內(nèi)容,有需要的小伙伴可以參考下2016-08-08
Android實現(xiàn)網(wǎng)絡加載圖片點擊大圖后瀏覽可縮放
這篇文章主要為大家詳細介紹了Android實現(xiàn)網(wǎng)絡加載圖片點擊大圖后瀏覽可縮放,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
利用libmp3lame實現(xiàn)在Android上錄音MP3文件示例
本篇文章主要介紹了利用Lame庫實現(xiàn)在Android上錄音MP3文件示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-03-03
Gradle?Build?Cache引發(fā)的Task緩存編譯問題
這篇文章主要為大家介紹了Gradle?Build?Cache引發(fā)的Task緩存編譯問題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-06-06
Android 實現(xiàn)沉浸式狀態(tài)欄的方法
沉浸式狀態(tài)欄的來源就是很多手機用的是實體按鍵,沒有虛擬鍵,于是開了沉浸模式就只有狀態(tài)欄消失了。下面腳本之家小編給大家介紹Android 實現(xiàn)沉浸式狀態(tài)欄,需要的朋友可以參考下2015-09-09
Android 中 requestWindowFeature()的應用
本文主要介紹 Android requestWindowFeature()方法,這里對 requestWindowFeature()方法進行詳解,對應用程序窗體顯示狀態(tài)的操作有進一步了解,希望能幫助有需要的小伙伴2016-07-07

