馬上加入Android 台灣中文網,立即免費下載應用遊戲。
您需要 登錄 才可以下載或查看,沒有帳號?註冊
x
如題 小弟正在整理以前學習的資料 發現有些東西無法正常活動
就是一般關於從sharedpreferences提取資料
我先上整體程式碼:
activity_login.xml
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout 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"
- tools:context=".LoginActivity">
- <EditText
- android:id="@+id/etUserName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:ems="10"
- android:hint="User Name"
- android:inputType="none|textPersonName"
- android:textSize="25sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.06" />
- <EditText
- android:id="@+id/etPassword"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="40dp"
- android:ems="10"
- android:hint="Password"
- android:inputType="none|textPersonName"
- android:textSize="25sp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/etUserName" />
- <Button
- android:id="@+id/btnSignIn"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:text="Sign In"
- android:textSize="25sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.502"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/etPassword"
- app:layout_constraintVertical_bias="0.239" />
- <TextView
- android:id="@+id/tvRegister"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:text="Register Here"
- android:textAlignment="center"
- android:textSize="25sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/btnSignIn"
- app:layout_constraintVertical_bias="0.325" />
- </android.support.constraint.ConstraintLayout>
複製代碼
activity_register.xml
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout 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"
- tools:context=".RegisterActivity">
- <EditText
- android:id="@+id/etMail"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:ems="10"
- android:hint="Email"
- android:inputType="none|textPersonName"
- android:textSize="25sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.281"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent"
- app:layout_constraintVertical_bias="0.104" />
- <EditText
- android:id="@+id/etUserName"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginBottom="364dp"
- android:ems="10"
- android:hint="UserName"
- android:inputType="none"
- android:textSize="25sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.276"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/etMail"
- app:layout_constraintVertical_bias="0.11" />
- <EditText
- android:id="@+id/etPassword"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="156dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:ems="10"
- android:hint="Password"
- android:inputType="none"
- android:textSize="25sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.279"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/etUserName"
- app:layout_constraintVertical_bias="0.096" />
- <EditText
- android:id="@+id/etAge"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:ems="10"
- android:hint="Age"
- android:inputType="textPersonName"
- android:textSize="25sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.279"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/etPassword"
- app:layout_constraintVertical_bias="0.084" />
- <Button
- android:id="@+id/btnRegister"
- android:layout_width="wrap_content"
- android:layout_height="52dp"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:text="Register"
- android:textSize="25sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.45"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/etAge"
- app:layout_constraintVertical_bias="0.251" />
- </android.support.constraint.ConstraintLayout>
複製代碼
activity_userhomepage.xml
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout 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"
- tools:context=".UserHomePage">
- <TextView
- android:id="@+id/etInPutUserName"
- android:layout_width="314dp"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:textSize="25sp"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </android.support.constraint.ConstraintLayout>
複製代碼
LoginActivity.java
- package com.example.loginpages;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- public class LoginActivity extends AppCompatActivity {
- public static final String PREF = "LOGIN_PREF";
- public static final String PREF_URN = "LOG_Usr_";
- public static final String PREF_PSW = "LOG_Psw_";
- public String uid;
- public boolean checkInputPageSouce = false;
- Button btnSignIn;
- EditText etUserName, etPassword;
- TextView tvRegister;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- findview();
- setListeners();
- }
- private void setListeners() {
- // 設定 btnSignIn 的監聽器為 signInListener
- btnSignIn.setOnClickListener(signInListener);
- // 設定 tvRegister 的監聽器為 registerListener
- tvRegister.setOnClickListener(registerListener);
- }
- public void findview ()
- {
- btnSignIn = (Button) findViewById(R.id.btnSignIn);
- etUserName = (EditText) findViewById(R.id.etUserName);
- etPassword = (EditText) findViewById(R.id.etPassword);
- tvRegister = (TextView) findViewById(R.id.tvRegister);
- }
- public void login(View v)
- {
- EditText etUserName = (EditText) findViewById(R.id.etUserName);
- EditText etPassword = (EditText) findViewById(R.id.etPassword);
- String uid = etUserName.getText().toString();
- String pw = etPassword.getText().toString();
- if (uid.equals("liea") && pw.equals("1234")) { //登入成功
- SharedPreferences setting =
- getSharedPreferences(PREF+uid, 0);
- setting.edit()
- .putString(PREF_URN, uid)
- .putString(PREF_PSW,pw)
- .commit();
- changeLogingWelcomePage();
- Toast.makeText(this, "帳號驗證成功", Toast.LENGTH_LONG).show();
- }else if (uid.equals("")|| pw.equals("")){
- Toast.makeText(this, "欄位不可空白", Toast.LENGTH_LONG).show();
- }else{
- Toast.makeText(this, "帳號驗證失敗", Toast.LENGTH_LONG).show();
- }
- }
- public void changeLogingWelcomePage(){
- Bundle bundle = new Bundle();
- uid = etUserName.getText().toString();
- bundle.putString("name", uid);
- bundle.putBoolean("checkInputPage",checkInputPageSouce);
- Intent intent = new Intent(LoginActivity.this, UserHomePage.class);
- intent.putExtras(bundle);
- startActivity(intent);
- }
- private View.OnClickListener signInListener =new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- login(view);
- }
- };
- private View.OnClickListener registerListener = new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
- startActivity(intent);
- }
- };
- }
複製代碼 RegisterActivity.java
- package com.example.loginpages;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class RegisterActivity extends AppCompatActivity {
- EditText etMail, etUserName, etPassword, etAge;
- String USERNAME,PASSWORD,EMAIL,AGE,name,age,email,password;
- Button btnRegister;
- private static final String TAG = "Login";
- public static final String PREF_USERNAME = "Login_Username";
- public static final String PREF_PASSWORD = "Login_Password";
- public static final String PREF_EMAIL = "Login_Email";
- public static final String PREF_AGE = "Login_Age";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_register);
- findview();
- btnRegister.setOnClickListener(registerListener);
- }
- View.OnClickListener registerListener = new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- Register(view);
- }
- };
- public void findview(){
- etMail = (EditText) findViewById(R.id.etMail);
- etUserName = (EditText) findViewById(R.id.etUserName);
- etPassword = (EditText) findViewById(R.id.etPassword);
- etAge = (EditText) findViewById(R.id.etAge);
- btnRegister = (Button) findViewById(R.id.btnRegister);
- }
- public void Register(View v) {
- Log.e(TAG, "Login-Register");
- USERNAME = etUserName.getText().toString();
- PASSWORD = etPassword.getText().toString();
- EMAIL = etMail.getText().toString();
- AGE = etAge.getText().toString();
- SharedPreferences setting = getSharedPreferences(TAG , 0);
- name = setting.getString( PREF_USERNAME+ etUserName.getText().toString(), "");
- age =setting.getString( PREF_AGE+ etAge.getText().toString(), "");
- password=setting.getString( PREF_AGE+etPassword.getText().toString(), "");
- email =setting.getString( PREF_EMAIL+ etMail.getText().toString(), "");
- if (USERNAME.equals("") || PASSWORD.equals("") || EMAIL.equals("") || AGE.equals("")) {
- Toast.makeText(RegisterActivity.this, "欄位不可空白", Toast.LENGTH_LONG).show();
- } else if (name.equals(etUserName.getText().toString())||email.equals(etMail.getText().toString())){
- Toast.makeText(RegisterActivity.this, "帳號已存在,請重新輸入", Toast.LENGTH_SHORT).show();
- } else {
- setting.edit()
- .putString(PREF_EMAIL + etMail.getText().toString(),etMail.getText().toString())
- .putString(PREF_USERNAME + etUserName.getText().toString(),etUserName.getText().toString())
- .putString(PREF_PASSWORD + etPassword.getText().toString(),etPassword.getText().toString() )
- .putString(PREF_AGE + etAge.getText().toString(), etAge.getText().toString()).commit();
- Toast.makeText(this, "註冊成功", Toast.LENGTH_LONG).show();
- Intent intent = new Intent(RegisterActivity.this, UserHomePage.class);
- Bundle bundle = new Bundle();
- bundle.putString("name", etUserName.getText().toString() );
- intent.putExtras(bundle);
- startActivity(intent);
- }
- }
- }
複製代碼
UserHomePageActivity.java
- package com.example.loginpages;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.widget.TextView;
- public class UserHomePage extends AppCompatActivity {
- TextView etInPutUserName;
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_userhomepage);
- findview();
- welcome();
- }
- public void findview()
- {
- etInPutUserName = findViewById(R.id.etInPutUserName);
- }
- private void welcome()
- {
- Bundle bundle = getIntent().getExtras();
- String uid =bundle.getString("name");
- etInPutUserName.setText("Welcome,"+uid);
- }
- }
複製代碼
手機運行問題
|
|