iT邦幫忙

2021 iThome 鐵人賽

DAY 30
0
自我挑戰組

Android kotlin &MVVM系列 第 30

Android學習筆記30

昨天做完了驗證碼,那今天就把他加入到帳號密碼之中
首先要先建立一個imageview來存放驗證碼,並把它設置為不可見

<LinearLayout
    android:id="@+id/code"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="50dp"
    android:visibility="invisible">

    <ImageView
        android:id="@+id/identifyingcode_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

接著判斷帳號密碼與驗證碼的邏輯

if (password.getText().toString().equals(userpassword) &&
        (account.getText().toString().equals(usercellphone) ||
        account.getText().toString().equals(useremail))){
        if(code.getVisibility()==View.VISIBLE) {
            if (usercode.getText().toString().toLowerCase().equals(realCode)) {
                if(checkbox.isChecked()){
                    SharedPreferences remember = getSharedPreferences("remember", MODE_PRIVATE);
                    remember.edit().putString("account", acc).putString("password", pass).putBoolean("ischeck",true).commit();
                    Bundle bundle = new Bundle();
                    bundle.putString("page", "1");
                    start(FragmentActivity.class, bundle, true);
                }
                else{
                    SharedPreferences remember=getSharedPreferences("remember",MODE_PRIVATE);
                    remember.edit().clear().commit();
                    Bundle bundle=new Bundle();
                    bundle.putString("page","1");
                    start(FragmentActivity.class,bundle,true);
                }
            }
            else{
                Toast.makeText(this,"驗證碼錯誤",Toast.LENGTH_SHORT).show();
            }
        }
        else{
            if (checkbox.isChecked()) {
                SharedPreferences remember = getSharedPreferences("remember", MODE_PRIVATE);
                remember.edit().putString("account", acc).putString("password", pass).putBoolean("ischeck",true).commit();
                Bundle bundle = new Bundle();
                bundle.putString("page", "1");
                start(FragmentActivity.class, bundle, true);
            }
            else{
                SharedPreferences remember = getSharedPreferences("remember", MODE_PRIVATE);
                remember.edit().clear().commit();
                Bundle bundle = new Bundle();
                bundle.putString("page", "1");
                start(FragmentActivity.class, bundle, true);
            }
        }
    }
    else{
        code.setVisibility(View.VISIBLE);
        visible.setVisibility(View.VISIBLE);
        Toast.makeText(this, "帳號密碼錯誤", Toast.LENGTH_SHORT).show();
    }

}

成果如下
https://ithelp.ithome.com.tw/upload/images/20211009/201417913bZmiuMlKS.png
接著帳號密碼錯誤的時候
https://ithelp.ithome.com.tw/upload/images/20211009/20141791DwbnFDySn4.png


上一篇
Android學習筆記29
系列文
Android kotlin &MVVM30

尚未有邦友留言

立即登入留言