綁定帳號登入

Android 台灣中文網

打印 上一主題 下一主題

[求助] 從sharedpreferences提取資料

[複製連結] 查看: 36557|回覆: 1|好評: 0
跳轉到指定樓層
樓主
lieak5922512 | 收聽TA | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
發表於 2019-8-22 22:53

馬上加入Android 台灣中文網,立即免費下載應用遊戲。

您需要 登錄 才可以下載或查看,沒有帳號?註冊

x
如題 小弟正在整理以前學習的資料 發現有些東西無法正常活動
就是一般關於從sharedpreferences提取資料

我先上整體程式碼:
activity_login.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context=".LoginActivity">

  8.     <EditText
  9.         android:id="@+id/etUserName"
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:layout_marginBottom="8dp"
  13.         android:layout_marginEnd="8dp"
  14.         android:layout_marginStart="8dp"
  15.         android:layout_marginTop="8dp"
  16.         android:ems="10"
  17.         android:hint="User Name"
  18.         android:inputType="none|textPersonName"
  19.         android:textSize="25sp"
  20.         app:layout_constraintBottom_toBottomOf="parent"
  21.         app:layout_constraintEnd_toEndOf="parent"
  22.         app:layout_constraintStart_toStartOf="parent"
  23.         app:layout_constraintTop_toTopOf="parent"
  24.         app:layout_constraintVertical_bias="0.06" />

  25.     <EditText
  26.         android:id="@+id/etPassword"
  27.         android:layout_width="wrap_content"
  28.         android:layout_height="wrap_content"
  29.         android:layout_marginEnd="8dp"
  30.         android:layout_marginStart="8dp"
  31.         android:layout_marginTop="40dp"
  32.         android:ems="10"
  33.         android:hint="Password"
  34.         android:inputType="none|textPersonName"
  35.         android:textSize="25sp"
  36.         app:layout_constraintEnd_toEndOf="parent"
  37.         app:layout_constraintStart_toStartOf="parent"
  38.         app:layout_constraintTop_toBottomOf="@+id/etUserName" />

  39.     <Button
  40.         android:id="@+id/btnSignIn"
  41.         android:layout_width="wrap_content"
  42.         android:layout_height="wrap_content"
  43.         android:layout_marginBottom="8dp"
  44.         android:layout_marginEnd="8dp"
  45.         android:layout_marginStart="8dp"
  46.         android:layout_marginTop="8dp"
  47.         android:text="Sign In"
  48.         android:textSize="25sp"
  49.         app:layout_constraintBottom_toBottomOf="parent"
  50.         app:layout_constraintEnd_toEndOf="parent"
  51.         app:layout_constraintHorizontal_bias="0.502"
  52.         app:layout_constraintStart_toStartOf="parent"
  53.         app:layout_constraintTop_toBottomOf="@+id/etPassword"
  54.         app:layout_constraintVertical_bias="0.239" />

  55.     <TextView
  56.         android:id="@+id/tvRegister"
  57.         android:layout_width="wrap_content"
  58.         android:layout_height="wrap_content"
  59.         android:layout_marginBottom="8dp"
  60.         android:layout_marginEnd="8dp"
  61.         android:layout_marginStart="8dp"
  62.         android:layout_marginTop="8dp"
  63.         android:text="Register Here"
  64.         android:textAlignment="center"
  65.         android:textSize="25sp"
  66.         app:layout_constraintBottom_toBottomOf="parent"
  67.         app:layout_constraintEnd_toEndOf="parent"
  68.         app:layout_constraintStart_toStartOf="parent"
  69.         app:layout_constraintTop_toBottomOf="@+id/btnSignIn"
  70.         app:layout_constraintVertical_bias="0.325" />

  71. </android.support.constraint.ConstraintLayout>
複製代碼

activity_register.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context=".RegisterActivity">
  8.     <EditText
  9.         android:id="@+id/etMail"
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:layout_marginBottom="8dp"
  13.         android:layout_marginEnd="8dp"
  14.         android:layout_marginStart="8dp"
  15.         android:layout_marginTop="8dp"
  16.         android:ems="10"
  17.         android:hint="Email"
  18.         android:inputType="none|textPersonName"
  19.         android:textSize="25sp"
  20.         app:layout_constraintBottom_toBottomOf="parent"
  21.         app:layout_constraintEnd_toEndOf="parent"
  22.         app:layout_constraintHorizontal_bias="0.281"
  23.         app:layout_constraintStart_toStartOf="parent"
  24.         app:layout_constraintTop_toTopOf="parent"
  25.         app:layout_constraintVertical_bias="0.104" />

  26.     <EditText
  27.         android:id="@+id/etUserName"
  28.         android:layout_width="wrap_content"
  29.         android:layout_height="wrap_content"
  30.         android:layout_marginStart="8dp"
  31.         android:layout_marginTop="8dp"
  32.         android:layout_marginEnd="8dp"
  33.         android:layout_marginBottom="364dp"
  34.         android:ems="10"
  35.         android:hint="UserName"
  36.         android:inputType="none"
  37.         android:textSize="25sp"
  38.         app:layout_constraintBottom_toBottomOf="parent"
  39.         app:layout_constraintEnd_toEndOf="parent"
  40.         app:layout_constraintHorizontal_bias="0.276"
  41.         app:layout_constraintStart_toStartOf="parent"
  42.         app:layout_constraintTop_toBottomOf="@+id/etMail"
  43.         app:layout_constraintVertical_bias="0.11" />

  44.     <EditText
  45.         android:id="@+id/etPassword"
  46.         android:layout_width="wrap_content"
  47.         android:layout_height="wrap_content"
  48.         android:layout_marginBottom="156dp"
  49.         android:layout_marginEnd="8dp"
  50.         android:layout_marginStart="8dp"
  51.         android:layout_marginTop="8dp"
  52.         android:ems="10"
  53.         android:hint="Password"
  54.         android:inputType="none"
  55.         android:textSize="25sp"
  56.         app:layout_constraintBottom_toBottomOf="parent"
  57.         app:layout_constraintEnd_toEndOf="parent"
  58.         app:layout_constraintHorizontal_bias="0.279"
  59.         app:layout_constraintStart_toStartOf="parent"
  60.         app:layout_constraintTop_toBottomOf="@+id/etUserName"
  61.         app:layout_constraintVertical_bias="0.096" />

  62.     <EditText
  63.         android:id="@+id/etAge"
  64.         android:layout_width="wrap_content"
  65.         android:layout_height="wrap_content"
  66.         android:layout_marginBottom="8dp"
  67.         android:layout_marginEnd="8dp"
  68.         android:layout_marginStart="8dp"
  69.         android:layout_marginTop="8dp"
  70.         android:ems="10"
  71.         android:hint="Age"
  72.         android:inputType="textPersonName"
  73.         android:textSize="25sp"
  74.         app:layout_constraintBottom_toBottomOf="parent"
  75.         app:layout_constraintEnd_toEndOf="parent"
  76.         app:layout_constraintHorizontal_bias="0.279"
  77.         app:layout_constraintStart_toStartOf="parent"
  78.         app:layout_constraintTop_toBottomOf="@+id/etPassword"
  79.         app:layout_constraintVertical_bias="0.084" />

  80.     <Button
  81.         android:id="@+id/btnRegister"
  82.         android:layout_width="wrap_content"
  83.         android:layout_height="52dp"
  84.         android:layout_marginBottom="8dp"
  85.         android:layout_marginEnd="8dp"
  86.         android:layout_marginStart="8dp"
  87.         android:layout_marginTop="8dp"
  88.         android:text="Register"
  89.         android:textSize="25sp"
  90.         app:layout_constraintBottom_toBottomOf="parent"
  91.         app:layout_constraintEnd_toEndOf="parent"
  92.         app:layout_constraintHorizontal_bias="0.45"
  93.         app:layout_constraintStart_toStartOf="parent"
  94.         app:layout_constraintTop_toBottomOf="@+id/etAge"
  95.         app:layout_constraintVertical_bias="0.251" />
  96. </android.support.constraint.ConstraintLayout>
複製代碼

activity_userhomepage.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context=".UserHomePage">
  8.     <TextView
  9.         android:id="@+id/etInPutUserName"
  10.         android:layout_width="314dp"
  11.         android:layout_height="wrap_content"
  12.         android:layout_marginBottom="8dp"
  13.         android:layout_marginEnd="8dp"
  14.         android:layout_marginStart="8dp"
  15.         android:layout_marginTop="8dp"
  16.         android:textSize="25sp"
  17.         app:layout_constraintBottom_toBottomOf="parent"
  18.         app:layout_constraintEnd_toEndOf="parent"
  19.         app:layout_constraintStart_toStartOf="parent"
  20.         app:layout_constraintTop_toTopOf="parent" />
  21. </android.support.constraint.ConstraintLayout>
複製代碼

LoginActivity.java
  1. package com.example.loginpages;

  2. import android.content.Intent;
  3. import android.content.SharedPreferences;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10. import android.widget.Toast;

  11. public class LoginActivity extends AppCompatActivity {

  12.     public static final String PREF = "LOGIN_PREF";
  13.     public static final String PREF_URN = "LOG_Usr_";
  14.     public static final String PREF_PSW = "LOG_Psw_";
  15.     public String uid;
  16.     public boolean checkInputPageSouce = false;

  17.     Button btnSignIn;

  18.     EditText etUserName, etPassword;

  19.     TextView tvRegister;

  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);

  23.         setContentView(R.layout.activity_login);

  24.         findview();
  25.         setListeners();

  26.     }


  27.     private void setListeners() {
  28.         // 設定 btnSignIn 的監聽器為 signInListener
  29.         btnSignIn.setOnClickListener(signInListener);
  30.         // 設定 tvRegister 的監聽器為 registerListener
  31.         tvRegister.setOnClickListener(registerListener);
  32.     }

  33.     public void findview ()
  34.     {
  35.         btnSignIn = (Button) findViewById(R.id.btnSignIn);

  36.         etUserName = (EditText) findViewById(R.id.etUserName);

  37.         etPassword = (EditText) findViewById(R.id.etPassword);

  38.         tvRegister = (TextView) findViewById(R.id.tvRegister);
  39.     }
  40.     public void login(View v)
  41.     {
  42.         EditText etUserName = (EditText) findViewById(R.id.etUserName);
  43.         EditText etPassword = (EditText) findViewById(R.id.etPassword);
  44.         String uid = etUserName.getText().toString();
  45.         String pw = etPassword.getText().toString();

  46.         if (uid.equals("liea") && pw.equals("1234")) { //登入成功

  47.             SharedPreferences setting =
  48.                     getSharedPreferences(PREF+uid, 0);

  49.             setting.edit()
  50.                     .putString(PREF_URN, uid)
  51.                     .putString(PREF_PSW,pw)
  52.                     .commit();

  53.             changeLogingWelcomePage();
  54.             Toast.makeText(this, "帳號驗證成功", Toast.LENGTH_LONG).show();
  55.         }else if (uid.equals("")|| pw.equals("")){
  56.             Toast.makeText(this, "欄位不可空白", Toast.LENGTH_LONG).show();
  57.         }else{
  58.             Toast.makeText(this, "帳號驗證失敗", Toast.LENGTH_LONG).show();
  59.         }
  60.     }
  61.     public void changeLogingWelcomePage(){
  62.         Bundle bundle = new Bundle();
  63.         uid = etUserName.getText().toString();
  64.         bundle.putString("name", uid);
  65.         bundle.putBoolean("checkInputPage",checkInputPageSouce);
  66.         Intent intent = new Intent(LoginActivity.this, UserHomePage.class);
  67.         intent.putExtras(bundle);
  68.         startActivity(intent);
  69.     }

  70.     private View.OnClickListener signInListener =new View.OnClickListener() {
  71.         @Override
  72.         public void onClick(View view) {
  73.             login(view);
  74.         }
  75.     };
  76.     private View.OnClickListener registerListener = new View.OnClickListener() {
  77.         @Override
  78.         public void onClick(View view) {
  79.             Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
  80.             startActivity(intent);
  81.         }
  82.     };


  83. }

複製代碼
RegisterActivity.java
  1. package com.example.loginpages;

  2. import android.content.Intent;
  3. import android.content.SharedPreferences;
  4. import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.util.Log;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.Toast;

  11. public class RegisterActivity extends AppCompatActivity {

  12.     EditText etMail, etUserName, etPassword, etAge;
  13.     String  USERNAME,PASSWORD,EMAIL,AGE,name,age,email,password;
  14.     Button btnRegister;

  15.     private static final String TAG = "Login";

  16.     public static final String PREF_USERNAME = "Login_Username";

  17.     public static final String PREF_PASSWORD = "Login_Password";

  18.     public static final String PREF_EMAIL = "Login_Email";

  19.     public static final String PREF_AGE = "Login_Age";



  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);

  23.         setContentView(R.layout.activity_register);

  24.         findview();


  25.         btnRegister.setOnClickListener(registerListener);
  26.     }
  27.     View.OnClickListener registerListener = new View.OnClickListener() {
  28.         @Override
  29.         public void onClick(View view) {
  30.             Register(view);

  31.         }
  32.     };


  33.     public void findview(){


  34.         etMail = (EditText) findViewById(R.id.etMail);

  35.         etUserName = (EditText) findViewById(R.id.etUserName);

  36.         etPassword = (EditText) findViewById(R.id.etPassword);

  37.         etAge = (EditText) findViewById(R.id.etAge);

  38.         btnRegister = (Button) findViewById(R.id.btnRegister);
  39.     }


  40.     public void Register(View v) {

  41.         Log.e(TAG, "Login-Register");

  42.         USERNAME = etUserName.getText().toString();

  43.         PASSWORD = etPassword.getText().toString();

  44.         EMAIL = etMail.getText().toString();

  45.         AGE = etAge.getText().toString();

  46.         SharedPreferences setting = getSharedPreferences(TAG , 0);
  47.         name = setting.getString( PREF_USERNAME+ etUserName.getText().toString(), "");
  48.         age =setting.getString( PREF_AGE+ etAge.getText().toString(), "");
  49.         password=setting.getString( PREF_AGE+etPassword.getText().toString(), "");
  50.         email =setting.getString( PREF_EMAIL+ etMail.getText().toString(), "");


  51.         if (USERNAME.equals("") || PASSWORD.equals("") || EMAIL.equals("") || AGE.equals("")) {
  52.             Toast.makeText(RegisterActivity.this, "欄位不可空白", Toast.LENGTH_LONG).show();


  53.         } else if (name.equals(etUserName.getText().toString())||email.equals(etMail.getText().toString())){
  54.             Toast.makeText(RegisterActivity.this, "帳號已存在,請重新輸入", Toast.LENGTH_SHORT).show();


  55.         } else {
  56.             setting.edit()
  57.                     .putString(PREF_EMAIL + etMail.getText().toString(),etMail.getText().toString())
  58.                     .putString(PREF_USERNAME + etUserName.getText().toString(),etUserName.getText().toString())
  59.                     .putString(PREF_PASSWORD + etPassword.getText().toString(),etPassword.getText().toString() )
  60.                     .putString(PREF_AGE + etAge.getText().toString(), etAge.getText().toString()).commit();

  61.             Toast.makeText(this, "註冊成功", Toast.LENGTH_LONG).show();
  62.             Intent intent = new Intent(RegisterActivity.this, UserHomePage.class);
  63.             Bundle bundle = new Bundle();
  64.             bundle.putString("name", etUserName.getText().toString() );
  65.             intent.putExtras(bundle);
  66.             startActivity(intent);
  67.         }

  68.     }

  69. }
複製代碼




UserHomePageActivity.java
  1. package com.example.loginpages;

  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.widget.TextView;

  5. public class UserHomePage extends AppCompatActivity {

  6.     TextView etInPutUserName;

  7.     @Override
  8.     protected void onCreate(Bundle savedInstanceState)
  9.     {
  10.         super.onCreate(savedInstanceState);
  11.         setContentView(R.layout.activity_userhomepage);

  12.         findview();
  13.         welcome();

  14.     }



  15.     public void findview()
  16.     {
  17.         etInPutUserName = findViewById(R.id.etInPutUserName);
  18.     }

  19.     private void welcome()
  20.     {
  21.         Bundle bundle = getIntent().getExtras();
  22.         String uid =bundle.getString("name");
  23.         etInPutUserName.setText("Welcome,"+uid);

  24.     }




  25. }
複製代碼

手機運行問題

「用Android 就來APK.TW」,快來加入粉絲吧!
Android 台灣中文網(APK.TW)
收藏收藏 分享分享 分享專題
用Android 就來Android 台灣中文網(https://apk.tw)
回覆

使用道具 舉報

沙發
jimmy122333 | 收聽TA | 只看該作者
發表於 2020-4-28 10:46
本帖最後由 jimmy122333 於 2020-4-28 10:47 編輯

從你的程式看來

帳號驗證成功這段你帳密要帶 liea/1234 才會成功啊你打ss當然不會驗證成功囉,根本還沒讀到SharedPreferences
而且你註冊頁的SharedPreferences KEY跟登入頁的key也不一樣
用Android 就來Android 台灣中文網(https://apk.tw)
回覆 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則