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



接下來,先寫首頁(yè)activity_main.xml頁(yè)面的內(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>
效果如圖:

首頁(yè)的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("首頁(yè)");
?
? ? ? ? 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 頁(yè)面:
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) {
? ? ? ? ? ? ? ? //判斷賬號(hào)密碼是否符合要求
? ? ? ? ? ? ? ? String account=etAccount.getText().toString();
? ? ? ? ? ? ? ? String password=etPassword.getText().toString();
? ? ? ? ? ? ? ? if (TextUtils.isEmpty(userName)){
? ? ? ? ? ? ? ? ? ? Toast.makeText(LoginActivity.this,"還沒有注冊(cè)賬號(hào)!",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","");
? ? ? ? //更新用戶名密碼(注冊(cè)的用戶名密碼)
? ? ? ? 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);
? ? }
?
? ? //還沒有賬號(hào)(跳轉(zhuǎn)到注冊(cè))
? ? 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);
?
? ? ? ? ? ? //更新用戶名密碼(注冊(cè)的用戶名密碼)
? ? ? ? ? ? userName=account;
? ? ? ? ? ? pass=password;
? ? ? ? }
? ? }
}接下來是RegisterActivity.java 頁(yè)面的內(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("注冊(cè)");
?
? ? ? ? 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,"請(qǐng)同意用戶協(xié)議!",Toast.LENGTH_LONG).show();
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? //存儲(chǔ)注冊(cè)的用戶名和密碼
? ? ? ? SharedPreferences spf=getSharedPreferences("spfRecorid",MODE_PRIVATE);
? ? ? ? SharedPreferences.Editor edit = spf.edit();
? ? ? ? edit.putString("account",name);
? ? ? ? edit.putString("password",pass);
? ? ? ? //注冊(cè)成功后,回到登錄頁(yè)面,數(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,"注冊(cè)成功!",Toast.LENGTH_LONG).show();
? ? ? ? this.finish();
? ? }
}到這里,驗(yàn)證登錄和記住密碼的功能就完成啦!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android用SharedPreferences實(shí)現(xiàn)登錄注冊(cè)注銷功能
- android實(shí)現(xiàn)注冊(cè)登錄程序
- Android實(shí)現(xiàn)登錄注冊(cè)功能
- Android基于Sqlite實(shí)現(xiàn)注冊(cè)和登錄功能
- Android實(shí)現(xiàn)注冊(cè)登錄界面的實(shí)例代碼
- Android設(shè)計(jì)登錄界面、找回密碼、注冊(cè)功能
- Android客戶端實(shí)現(xiàn)注冊(cè)、登錄詳解(1)
- Android登錄注冊(cè)功能 數(shù)據(jù)庫(kù)SQLite驗(yàn)證
- Android Studio連接SQLite數(shù)據(jù)庫(kù)的登錄注冊(cè)實(shí)現(xiàn)
- Android使用http實(shí)現(xiàn)注冊(cè)登錄功能
相關(guān)文章
利用Android畫圓弧canvas.drawArc()實(shí)例詳解
這篇文章主要給大家介紹了關(guān)于利用Android畫圓弧canvas.drawArc()的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的理解和學(xué)習(xí)具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
Android 基于agora 開發(fā)視頻會(huì)議的代碼
這篇文章主要介紹了Android 基于agora 開發(fā)視頻會(huì)議,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Android 通過httppost上傳文本文件到服務(wù)器的實(shí)例代碼
這篇文章主要介紹了Android 通過httppost上傳文本文件到服務(wù)器的實(shí)例代碼,非常簡(jiǎn)單易懂,非常實(shí)用,需要的朋友可以參考下2016-08-08
基于Android在布局中動(dòng)態(tài)添加view的兩種方法(總結(jié))
下面小編就為大家?guī)硪黄贏ndroid在布局中動(dòng)態(tài)添加view的兩種方法(總結(jié))。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
Android組合控件實(shí)現(xiàn)功能強(qiáng)大的自定義控件
這篇文章主要介紹了Android組合控件實(shí)現(xiàn)功能強(qiáng)大的自定義控件的相關(guān)資料,需要的朋友可以參考下2016-05-05
android實(shí)現(xiàn)定時(shí)拍照功能
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)定時(shí)拍照功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android 對(duì)話框 Dialog使用實(shí)例講解
對(duì)話框是在當(dāng)前的頁(yè)面之上彈出的小窗口, 用于顯示一些重要的提示信息, 提示用戶的輸入,確認(rèn)信息,或顯示某種狀態(tài).如 : 顯示進(jìn)度條對(duì)話框, 退出提示.接下來通過本文給大家介紹android dialog對(duì)話框知識(shí),感興趣的朋友一起看看吧2016-09-09
Android 二維碼 生成和識(shí)別二維碼 附源碼下載
這篇文章主要介紹了Android 生成和識(shí)別二維碼的方法,提供源碼下載,需要的朋友可以參考下。2016-06-06

