-5

我是android的初学者..我只擅长设计android应用程序..我不知道共享偏好..我第一次必须使用共享偏好,。所以任何人都可以在我的代码中做到这一点,以便我可以实现。以下是我的代码,请在我的代码中进行更改:告诉我如何在按钮 a 的单击上保存值,我将其临时存储在不同的变量中。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class newpage extends Activity {
    String un = null;
    String pass= null;
    String fname= null;
    String lname= null;
    String age= null;
    String city= null;
    String coun= null;
    String email= null;
    String pno= null;
    String repass= null;

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


    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,  
         WindowManager.LayoutParams.FLAG_FULLSCREEN);

            setContentView(R.layout.create_acc);

            final Button b = (Button) findViewById(R.id.button3);
             b.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View v) {

                     Intent i = new Intent(newpage.this, SignIn.class);
                        i.addFlags(
                        Intent.FLAG_ACTIVITY_CLEAR_TOP |
                        Intent.FLAG_ACTIVITY_NEW_TASK);                 
                        startActivity(i);
                 }
             });

             final Button c = (Button) findViewById(R.id.button2);
             c.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View v) {

                     Intent i = new Intent(newpage.this, CreateAcc.class);
                    i.addFlags(
                        Intent.FLAG_ACTIVITY_CLEAR_TOP |
                        Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(i);
                     }
             });

             final Button a = (Button) findViewById(R.id.button1);
             a.setOnClickListener(new View.OnClickListener() {

                    @SuppressLint("ShowToast")
                    public void onClick(View v){

                        EditText input1= (EditText)findViewById(R.id.fname1); 
                        fname = input1.getEditableText().toString();

                        EditText input2= (EditText) findViewById(R.id.lname1); 
                        lname = input2.getEditableText().toString();

                        EditText input3= (EditText) findViewById(R.id.age1); 
                        age = input3.getEditableText().toString();

                        EditText input4= (EditText) findViewById(R.id.city1); 
                        city = input4.getEditableText().toString();

                        EditText input5= (EditText) findViewById(R.id.country1);  
                        coun = input5.getEditableText().toString();

                        EditText input6= (EditText) findViewById(R.id.email1); 
                        email = input6.getEditableText().toString();

                        EditText input7= (EditText) findViewById(R.id.pno1);  
                        pno = input7.getEditableText().toString();

                        EditText input8= (EditText) findViewById(R.id.uname1);
                        un = input8.getEditableText().toString();

                        EditText input9= (EditText) findViewById(R.id.passwd1);
                        pass = input9.getEditableText().toString();

                        EditText input10= (EditText) findViewById(R.id.repass1); 
                         repass = input10.getEditableText().toString();

                        if((fname!=null) && (lname!=null) && (age!=null) && (city!=null) && (coun!=null) && (email!=null) && (pno!=null) && (un!=null) && (pass!=null) && (repass!=null))   
                        {   
                            if(pass == repass)
                            {
                                Toast.makeText(newpage.this, ".....Data Saved....." ,Toast.LENGTH_LONG).show();
                                Intent in = new Intent(newpage.this, SignIn.class);
                                    in.addFlags(
                                    Intent.FLAG_ACTIVITY_CLEAR_TOP |
                                    Intent.FLAG_ACTIVITY_NEW_TASK);
                                    startActivity(in);
                            }
                            else
                            {
                                Toast.makeText(newpage.this, ".....Password Doesn't Match....." ,Toast.LENGTH_LONG).show();
                            }                   
                        }
                        else
                        {
                        Toast.makeText(newpage.this, ".....Complete the Form Please....." ,Toast.LENGTH_LONG).show();
                        }
                  }
             });            
    }
}
4

2 回答 2

0

它们真的很简单。您刚刚调用了SharedPreferences该类来保存和检索值。最好只存储简单的值,任何复杂的都应该使用SQLlite

此代码将保存电子邮件值:

 SharedPreferences settings = getSharedPreferences("[unique id]", Context.MODE_PRIVATE);
 SharedPreferences.Editor editor = settings.edit();

 editor.putString("email", email);
 editor.commit();

你只需要putStrings为其他价值创造更多。

这将检索值:

 String email = settings.getString("email", null);
于 2013-04-05T17:36:25.910 回答
0

读或写给SharedPreferences你首先需要得到SharedPreferences

SharedPreferences prefs = getSharedPreferences(TAG, Context.MODE_PRIVATE);

一旦你有了SharedPreferences你就可以写信给他们

prefs.edit().putString(KEY, VALUE).commit();

或者你可以从他们那里读到

prefs.getString(KEY, VALUE);

差不多就是这样。首选项中的 TAG 就是您要调用的名称SharedPreferences- 它可以是任何字符串。一切都保存为键值对。您需要弄清楚它在代码中的位置,因为我不知道您要存储什么。

于 2013-04-05T17:36:46.103 回答