【问题标题】:How to use data of an activity in an other?如何使用另一个活动的数据?
【发布时间】:2015-02-13 02:14:45
【问题描述】:

假设我有 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 的值

提前谢谢你!

【问题讨论】:

标签: android android-intent android-activity


【解决方案1】:

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

例如。在活动 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

在活动 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
}

欲了解更多详情,请访问此链接: Shared Preferences in Android

【讨论】:

  • 确实我正在使用 SharedPreferences,我会试试这个并告诉你。感谢 PPD 和 user2653581
【解决方案2】:
Intent in = new Intent(activity1.this, activity2.class);
in.putExtra("message", message);
startActivity(in);

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

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

如果您在活动 1 中获取一些数据。但是从activity1调用activity3,然后从activity3调用activity2。如果您希望在 activity2 中获得该数据,那么您可以使用 sharepreference:

在您获取数据的活动1 中,您存储的数据如下:

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

在activity2中你会得到它:

SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 

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

【讨论】:

  • 我必须写行 startActivity(in) 吗?
猜你喜欢
  • 2019-05-30
  • 1970-01-01
  • 2012-11-19
  • 1970-01-01
  • 2021-01-20
  • 2015-12-31
  • 2020-02-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多