如何在另一个中使用 activity 的数据?

How to use data of an activity in an other?

假设我有 2 个活动 activity1 和 activity2。在 activity1 中,我有一个简单的表单。这是 xml 文件。

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="25dp"
        android:layout_marginTop="25dp"
        android:text="@string/m_informations"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/FirstName"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />

    </EditText>

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/Name"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/Email"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/confirm" />

</LinearLayout>

我现在想在 activity2 中使用这些 Edittexts 的数据,知道这两个活动之间没有链接。我的意思是在这两个活动之间,还有其他活动开始。

我想在我在 activity2 中创建的特定函数中使用这些数据。

假设它是这样的:

void write(String st)
{
 st = edittext1 value
}

st 必须例如取 activity1 的 edittext1 的值

提前致谢!

Intent in = new Intent(activity1.this, activity2.class);
in.putExtra("message", message);
startActivity(in);

在 acticity2 中,您会得到如下数据:

Bundle bundle = getIntent().getExtras();
String message = bundle.getString("message");

如果您正在 activity1 中获取一些数据。但是从 activity1 调用 activity3,然后从 activity3 调用 activity2。你想要 activity2 中的数据然后你可以使用 sharepreference:

在您获取数据的 activity1 中,您存储的数据如下:

SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("myName", "PPD");
editor.putInt("id", 1);
editor.commit();

在活动 2 中,您将得到它:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 

  String name = prefs.getString("myName", "No Name");
  int idName = prefs.getInt("id", 0); 

使用共享首选项在另一个 activity 中存储和访问数据。 在 shared Preference 中,我们创建了一个只能由当前应用程序访问的文件,我们在其中添加键值对,然后在需要时加载它们

例如。在 activity 1:

EditText et1=(EditText) findViewById(R.id.editText1);
String et1_val=et1.getText().toString();
SharedPreferences pref=getSharedPreferences("MYPREF", 0);
//creating a shared preference with the name "MYPREF" and 0 is for default settings

final SharedPreferences.Editor editor=pref.edit();
//creating an editor to add the KeyValue Pairs to the preferences file.

editor.putString("et1_val",et1_val);
//adding the keyvalue pairs to the editior.

editor.commit();
//To close and commit the changes in the shared preference

在 activity 2 中: 你的方法是这样的:

void write(String st)
{

SharedPreferences pref=getSharedPreferences("MYPREF", 0);
//getting reference to the shared preference we created.

String edit_text1_value=pref.getString("et1_val","");
//"" is the default value when the requested key has no value associated with it.

st=edit_text1_value;
//copiying the value of the edit text 1 into st
}

更多详情请访问此link: Shared Preferences in Android