2017-06-08 4 views
1

Ich habe eine Anmeldeseite erstellt, die eine Verbindung zu einer MySQL-Datenbank herstellt. In der activity_user_login.xml habe ich 2 editText für E-Mail und Passwort und ein Login Botton, ich habe ein Kontrollkästchen für die gespeicherten Daten hinzugefügt. Ich möchte E-Mail und Passwort speichern.
Allerdings kann ich die sharedPreference nicht hinzufügen. Unter dem vollständigen Code .. Sie können mir helfen? DankeSo verwenden Sie eine gemeinsame Einstellung zum Kontrollkästchen - Android Studio

activity_user_login.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:id="@+id/activity_user_login" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.myapp.UserLoginActivity" 
    android:background="#FF5722" 
    android:padding="20dp"> 

    <TextView 
     android:text="User Login Form" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:id="@+id/textView" 
     android:gravity="center" 
     android:textSize="20dp" 
     android:textColor="#fff"/> 

    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:background="#fbfefd" 
     android:inputType="textPersonName" 
     android:ems="10" 
     android:layout_below="@+id/textView" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="20dp" 
     android:id="@+id/email" 
     android:hint="Enter Your Email Here" 
     android:gravity="center" 
     /> 

    <EditText 
     android:layout_width="fill_parent" 
     android:layout_height="50dp" 
     android:background="#fbfefd" 
     android:inputType="textPassword" 
     android:ems="10" 
     android:layout_below="@+id/email" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="20dp" 
     android:id="@+id/password" 
     android:hint="Enter Your Password Here" 
     android:gravity="center"/> 

    <Button 
     android:text="Log In Here" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/password" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="20dp" 
     android:id="@+id/Login" /> 

    <CheckBox 
     android:id="@+id/saveLoginCheckBox" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/editTextPassword" 
     android:text="Save Login?" 
     android:textColor="#FFF" /> 

</RelativeLayout> 

UserLoginActivity.java

package com.myapp.myappcode; 

import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.text.TextUtils; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 
import java.util.HashMap; 


public class UserLoginActivity extends AppCompatActivity { 

    EditText Email, Password; 
    Button LogIn ; 
    String PasswordHolder, EmailHolder; 
    String finalResult ; 
    String HttpURL = "http://mysite/User/UserLogin.php"; 
    Boolean CheckEditText ; 
    ProgressDialog progressDialog; 
    HashMap<String,String> hashMap = new HashMap<>(); 
    HttpParse httpParse = new HttpParse(); 
    public static final String UserEmail = ""; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_user_login); 

     Email = (EditText)findViewById(R.id.email); 
     Password = (EditText)findViewById(R.id.password); 
     LogIn = (Button)findViewById(R.id.Login); 

     LogIn.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       CheckEditTextIsEmptyOrNot(); 

       if(CheckEditText){ 

        UserLoginFunction(EmailHolder, PasswordHolder); 

       } 
       else { 

        Toast.makeText(UserLoginActivity.this, "Please fill all form fields.", Toast.LENGTH_LONG).show(); 

       } 

      } 
     }); 
    } 
    public void CheckEditTextIsEmptyOrNot(){ 

     EmailHolder = Email.getText().toString(); 
     PasswordHolder = Password.getText().toString(); 

     if(TextUtils.isEmpty(EmailHolder) || TextUtils.isEmpty(PasswordHolder)) 
     { 
      CheckEditText = false; 
     } 
     else { 

      CheckEditText = true ; 
     } 
    } 

    public void UserLoginFunction(final String email, final String password){ 

     class UserLoginClass extends AsyncTask<String,Void,String> { 

      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 

       progressDialog = ProgressDialog.show(UserLoginActivity.this,"Loading Data",null,true,true); 
      } 

      @Override 
      protected void onPostExecute(String httpResponseMsg) { 

       super.onPostExecute(httpResponseMsg); 

       progressDialog.dismiss(); 

       if(httpResponseMsg.equalsIgnoreCase("Data Matched")){ 

        finish(); 

        Intent intent = new Intent(UserLoginActivity.this, DashboardActivity.class); 

        intent.putExtra(UserEmail,email); 

        startActivity(intent); 

       } 
       else{ 

        Toast.makeText(UserLoginActivity.this,httpResponseMsg,Toast.LENGTH_LONG).show(); 
       } 

      } 

      @Override 
      protected String doInBackground(String... params) { 

       hashMap.put("email",params[0]); 

       hashMap.put("password",params[1]); 

       finalResult = httpParse.postRequest(hashMap, HttpURL); 

       return finalResult; 
      } 
     } 

     UserLoginClass userLoginClass = new UserLoginClass(); 

     userLoginClass.execute(email,password); 
    } 

} 
+0

Zu welchem ​​Zweck möchten Sie sharedPreference ... verwenden? – Syed

+0

willst du auch mehrere E-Mails und Passwörter speichern ??? –

+0

Ich möchte die gemeinsame Voreinstellung verwenden, damit Benutzer die Anmeldung speichern können. – LittleGuy

Antwort

0
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE); 
SharedPreferences.Editor editor = sharedpreferences.edit(); 

// get e mail and pass on button click and save it like this 
      editor.putString(pass, ps); 
      editor.putString(Email, e); 
      editor.commit(); 

und es zurück zu bekommen:

String n = ed1.getText().toString(); 
      String pass = ed2.getText().toString(); 
      String e = ed3.getText().toString(); 

hier ist das Beispiel link.

+0

Nachdem Sie die Werte gespeichert haben, wie füge ich sie wieder in EditText ein? – LittleGuy

+0

holen Sie die Werte als Beispielcode oben und edittext.setText (yourValue); – Simo

0

Checkbox wird dann Werte an die Gemeinsame Präferenz

die Werte aus Gemeinschafts-Preference

SharedPreferences pref=getSharedPreferences("values", Context.MODE_PRIVATE); 
String useremail=pref.getString("email","def_val"); 
String password=pref.getString("pass","def_val"); 

ersetzen erhalten setzen geprüft

Setzen Sie die Werte Gemeinsame Preference

SharedPreferences pref=getSharedPreferences("values",Context.MODE_PRIVATE); 
pref.edit().putString("email", Email.getText().toString()); 
pref.edit().putString("pass", Password.getText().toString()); 
pref.apply(); 

DEF_VAL mit leer oder irgendetwas

+0

kannst du mir helfen? Ich verstehe nicht, wo es ist, den Code zu setzen. Vielen Dank – LittleGuy

+0

Vielen Dank Raj, aber ich habe einen Fehler pref.apply() .... anwenden kann nicht Methode – LittleGuy

+0

Scusam Ray zum Vergnügen können Sie mir den ganzen Code von UserLoginActivity.java schreiben, damit ich Fehler zu vermeiden. Vielen Dank für die Unordnung – LittleGuy

Verwandte Themen