Я искал в stackoverflow, и я не мог найти ничего подобного. Я хочу создать систему регистрации, такую как регистрация и логин. Но следующий код не работает при нажатии Login. Вероятно, ошибка в логике if. пожалуйста помоги'.Система регистрации общих настроек в android
MainActivity.java
package com.example.asifsabir.sharedpref;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
EditText ed1,ed2;
Button b1,b2,b3;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String email = "emailKey";
public static final String password = "passwordKey";
public static final String safety = "safetyKey";
SharedPreferences sharedpreferences;
SharedPreferences sharedpreferences2;
//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1=(EditText)findViewById(R.id.editText1);
ed2=(EditText)findViewById(R.id.editText2);
b1=(Button)findViewById(R.id.button);
b2=(Button)findViewById(R.id.button2);
b3=(Button)findViewById(R.id.button3);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = ed1.getText().toString();
String password = ed2.getText().toString();
if(sharedpreferences.getString(password,"").equals(password))
Toast.makeText(MainActivity.this, "Logged on Successful", Toast.LENGTH_SHORT).show();
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = ed1.getText().toString();
String password = ed2.getText().toString();
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(email,email);
editor.putString(password,password);
editor.commit();
Toast.makeText(MainActivity.this,"Thanks! Signed up",Toast.LENGTH_LONG).show();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Shared Preference \n login system"
android:id="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="35dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:layout_below="@+id/textView1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="email id: " />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editText2"
android:layout_below="@+id/editText1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="password: " />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="@+id/button"
android:layout_below="@+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign Up"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="forgot password?"
android:id="@+id/button3"
android:layout_below="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
</RelativeLayout>
Еще одна проблема: я зарегистрировался по электронной почте: asif и пароль: 12345, а также по электронной почте: david pasword: 555, но я могу войти с помощью электронной почты: asif и password: 555, как это можно решить? –
Итак, ваша проблема в том, что любая комбинация электронной почты и паролей регистрирует вас, правильно? –
Вы используете локальные и глобальные переменные для электронной почты и паролей с тем же именем, вместо этого измените глобальные значения на emailKey, passwordKeyand здесь: sharedpreferences.getString (пароль, ""). Equals (password)), измените на sharedpreferences.getString (passwordKey , ""). равен (пароль)) –