如何保持活动之间的会话?

时间:2014-02-03 05:58:16

标签: android session android-intent sharedpreferences key-value-store

我为用户注册设计了3个页面。我想保留要存储的值,直到用户按下第3页中的提交按钮。我在每个页面中都有下一个和上一个按钮。当我单击第二页中的上一个按钮时,将加载用户在第一页中输入的所有值,当他点击下一个按钮时,将加载第二页中给出的详细信息。我怎样才能做到这一点?有人可以帮忙吗?

这是我试过的代码,

    private EditText username;
    private EditText email;
    private EditText mobile;
    private EditText firstname;
    public static ArrayList<String> userInfo = new ArrayList<String>();
    String userinfo[];
    private Button next1;
    final Context context=this;
    public static final String MyPREFERENCES = "MyPrefs" ;
    public static final String Name = "nameKey"; 
    public static final String Phone = "phoneKey"; 
    public static final String Email = "emailKey"; 
    public static final String user = "userKey"; 
    SharedPreferences sharedpreferences;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.register);
    ActionBar actbar=getActionBar();
    actbar.show();

    getActionBar().setDisplayHomeAsUpEnabled(true);
    Button clr=(Button)findViewById(R.id.cler);
    sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

     if (sharedpreferences.contains(Name))
     {
     firstname.setText(sharedpreferences.getString(Name, ""));

     }
     if (sharedpreferences.contains(Phone))
     {
     mobile.setText(sharedpreferences.getString(Phone, ""));

     }
      if (sharedpreferences.contains(Email))
     {
     email.setText(sharedpreferences.getString(Email, ""));

      }
      if (sharedpreferences.contains(user))
     {
     username.setText(sharedpreferences.getString(user, ""));

     }
     clr.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
     firstname = (EditText) findViewById(R.id.firstnam);
     username  = (EditText) findViewById(R.id.frstname);
     email = (EditText) findViewById(R.id.mail);
     mobile = (EditText) findViewById(R.id.mobile);
    username.setText("");
   email.setText("");
   mobile.setText("");
   firstname.setText("");

   }

     });
    registerValid();
  }
   public void run(View view){
    String n  = firstname.getText().toString();
    String ph  = mobile.getText().toString();
    String e  = email.getText().toString();
    String u  = username.getText().toString();

    Editor editor = sharedpreferences.edit();
    editor.putString(Name, n);
    editor.putString(Phone, ph);
    editor.putString(Email, e);
    editor.putString(user, u);


    editor.commit(); 

 }

布局文件,

    <?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:background="@drawable/backgrou"
    android:orientation="vertical" >

<Button
    android:id="@+id/next"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="70sp"
    android:layout_height="40sp"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/firstnam"
    android:layout_marginBottom="64dp"
    android:background="@drawable/buttonback"
    android:text="@string/next2"
    android:textColor="#ffffff" />

<EditText
    android:id="@+id/frstname"
    android:layout_width="180sp"
    android:layout_height="40sp"
    android:layout_above="@+id/mobile"
    android:layout_alignLeft="@+id/mobile"
    android:layout_marginBottom="26dp"
    android:background="@drawable/edittext"
    android:ems="10"
    android:digits="abcdefghijklmnopqrstuvwxyz0123456789.-@_"
    android:inputType="textPersonName" />

    <TextView
    android:id="@+id/firstname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/frstname"
    android:layout_alignParentLeft="true"
    android:layout_marginBottom="40dp"
    android:layout_marginLeft="20dp"
    android:text="@string/firname"
    android:textColor="#ffffff" />

<TextView
    android:id="@+id/mob"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/next"
    android:layout_alignRight="@+id/fname"
    android:layout_marginBottom="144dp"
    android:text="@string/mobile"
    android:textColor="#ffffff" />

   <EditText
    android:id="@+id/mobile"
    android:layout_width="180sp"
    android:layout_height="40sp"
    android:layout_alignLeft="@+id/mail"
    android:layout_alignTop="@+id/mob"
    android:background="@drawable/edittext"
    android:ems="10"
    android:maxLength="10"
    android:inputType="phone" />

    <Button
    android:id="@+id/cler"
    style="?android:attr/buttonStyleSmall"
    android:layout_width="70sp"
    android:layout_height="40sp"
    android:layout_alignBaseline="@+id/next"
    android:layout_alignBottom="@+id/next"
    android:layout_alignLeft="@+id/mob"
    android:background="@drawable/buttonback"
    android:text="@string/reset"
    android:textColor="#ffffff" />

<EditText
    android:id="@+id/firstnam"
    android:layout_width="180sp"
    android:layout_height="40sp"
    android:layout_alignLeft="@+id/frstname"
    android:layout_alignTop="@+id/firstname"
    android:background="@drawable/edittext"
    android:ems="10"
    android:inputType="textPersonName" >  <requestFocus /></EditText>

<TextView
    android:id="@+id/fname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/firstname"
    android:layout_below="@+id/firstname"
    android:text="@string/fname"
    android:textColor="#ffffff" />

<TextView
    android:id="@+id/email"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/mob"
    android:layout_below="@+id/mobile"
    android:layout_marginTop="20dp"
    android:text="@string/mail"
    android:textColor="#ffffff" />

   <EditText
    android:id="@+id/mail"
    android:layout_width="180sp"
    android:layout_height="40sp"
    android:layout_alignTop="@+id/email"
    android:layout_toRightOf="@+id/cler"
    android:background="@drawable/edittext"
    android:ems="10"
    android:inputType="textEmailAddress" />

   </RelativeLayout>

2 个答案:

答案 0 :(得分:1)

通过捆绑传递值并且不完成活动。 通过这种方式,当你使用反向值返回时就到位了。 如果你想在你的活动中使用软后退按钮,那么在onClick中使用super.onBackPressed

答案 1 :(得分:1)

好的,请使用以下代码。

<强> XML

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/scrollView1"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      tools:context=".DisplayContact" >

<RelativeLayout
   android:layout_width="match_parent"
   android:layout_height="370dp"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   >

   <EditText
      android:id="@+id/editTextName"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignParentLeft="true"
      android:layout_marginTop="5dp"
      android:layout_marginLeft="82dp"
      android:ems="10"
      android:inputType="text" >

   </EditText>

   <EditText
      android:id="@+id/editTextEmail"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/editTextStreet"
      android:layout_below="@+id/editTextStreet"
      android:layout_marginTop="22dp"
      android:ems="10"
      android:inputType="textEmailAddress" />

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/editTextName"
      android:layout_alignParentLeft="true"
      android:text="@string/name"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/editTextCity"
      android:layout_alignParentBottom="true"
      android:layout_marginBottom="28dp"
      android:onClick="run"
      android:text="@string/save" />

   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/editTextEmail"
      android:layout_alignLeft="@+id/textView1"
      android:text="@string/email"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <TextView
      android:id="@+id/textView5" 
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBottom="@+id/editTextPhone"
      android:layout_alignLeft="@+id/textView1"
      android:text="@string/phone"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <TextView
      android:id="@+id/textView4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_above="@+id/editTextEmail"
      android:layout_alignLeft="@+id/textView5"
      android:text="@string/street"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <EditText
      android:id="@+id/editTextCity"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignRight="@+id/editTextName"
      android:layout_below="@+id/editTextEmail"
      android:layout_marginTop="30dp"
      android:ems="10"
      android:inputType="text" />

   <TextView
      android:id="@+id/textView3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignBaseline="@+id/editTextCity"
      android:layout_alignBottom="@+id/editTextCity"
      android:layout_alignParentLeft="true"
      android:layout_toLeftOf="@+id/editTextEmail"
      android:text="@string/country"
      android:textAppearance="?android:attr/textAppearanceMedium" />

   <EditText
      android:id="@+id/editTextStreet"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/editTextName"
      android:layout_below="@+id/editTextPhone"
      android:ems="10"
      android:inputType="text" >

      <requestFocus />
   </EditText>

   <EditText
      android:id="@+id/editTextPhone"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_alignLeft="@+id/editTextStreet"
      android:layout_below="@+id/editTextName"
      android:ems="10"
      android:inputType="phone|text" />

   </RelativeLayout>

   </ScrollView>
public class MainActivity extends Activity {

   TextView name ;
   TextView phone;
   TextView email;
   TextView street;
   TextView place;
   public static final String MyPREFERENCES = "MyPrefs" ;
   public static final String Name = "nameKey"; 
   public static final String Phone = "phoneKey"; 
   public static final String Email = "emailKey"; 
   public static final String Street = "streetKey"; 
   public static final String Place = "placeKey"; 

   SharedPreferences sharedpreferences;

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

      name = (TextView) findViewById(R.id.editTextName);
      phone = (TextView) findViewById(R.id.editTextPhone);
      email = (TextView) findViewById(R.id.editTextStreet);
      street = (TextView) findViewById(R.id.editTextEmail);
      place = (TextView) findViewById(R.id.editTextCity);

next1 = (Button) findViewById(R.id.next); 
next1.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View view) { 
String n = firstname.getText().toString(); 
String ph = mobile.getText().toString(); 
String e = email.getText().toString(); 
String u = username.getText().toString(); 

Editor editor = sharedpreferences.edit(); 
editor.putString(Name, n); 
editor.putString(Phone, ph); 
editor.putString(Email, e); 
editor.putString(user, u); 


editor.commit(); 

startActivity(new Intent(SignupActivity.this,NextActivity.class)); 

} 
});

      sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

      }



 @Override 
protected void onResume() { 
super.onResume(); 
if (sharedpreferences.contains(Name)) 
{ 
firstname.setText(sharedpreferences.getString(Name, "")); 

} 
if (sharedpreferences.contains(Phone)) 
{ 
mobile.setText(sharedpreferences.getString(Phone, "")); 

} 
if (sharedpreferences.contains(Email)) 
{ 
email.setText(sharedpreferences.getString(Email, "")); 

} 
if (sharedpreferences.contains(user)) 
{ 
username.setText(sharedpreferences.getString(user, "")); 

} 
} 

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
   }

}

现在,在下一个按钮的点击事件转到下一个活动以及下一个活动的上一个按钮时,当您参加此活动时,此数据将与编辑文本中的数据一样。