0

您好,希望您能帮到我:

我有三个活动:Activity1 --> Activity2 --> Activity3

在 Activity1 和 2 中,我有一个 Intent,它从 EditText Extra 中放置了一个字符串。

在 Activity3 中,我想接收两个字符串,但是当我尝试 Activity3 时,只接收来自 Activity2 的 Intent (上一个活动)。

这里是来自 Activity1 的代码:

public void onClick(View v) {

    Intent intent = new Intent (this, Activity2.class);
    EditText et1 = (EditText) findViewById(R.id.editText1);
    String name = et1.getText().toString();

        if (name.length() == 0) {
            new AlertDialog.Builder(this).setMessage(
                    R.string.error_name_missing).setNeutralButton(
                    R.string.error_ok,
                    null).show();
            return;
        }

        intent.putExtra(konto_name1, name);
        startActivity(intent);

    }   

从活动2:

public void onClick(View v) {

        Intent intent = new Intent (this, Activity3.class); 
        EditText et2 = (EditText) findViewById(R.id.editText2);

        String value = et2.getText().toString();

        if (value.length() == 0) {
            new AlertDialog.Builder(this).setMessage(
                    R.string.error_value_missing).setNeutralButton(
                    R.string.error_ok,
                    null).show();
            return;
        }

        intent.putExtra(start_value1, value);
        startActivity(intent);      
    }
4

3 回答 3

1

获取您的第一个活动意图Acitivity2.java并再次将其传递给Activity3.java

Activity1.java

 Intent intent = new Intent (this, Activity2.class);
 intent.putExtra(konto_name1, name);
 startActivity(intent);

Activity2.java

  Intent intent = new Intent (this, Activity3.class);
final String firstActivityValue = getIntent().getStringExtra("name");
    intent.putExtra(start_value1, value);
    intent.putExtra(start_value2, firstActivityValue);
    startActivity(intent);

Activity3.java现在您可以使用 getIntent()获得这两个值。

希望能帮助到你。

于 2013-08-12T13:06:00.620 回答
1

正如其他答案所讨论的那样,您可以从一个意图传递到另一个意图。但是,您也可以使用SharedPreferences它来使其更易于使用。

SharedPreferences prefs = getDefaultSharedPreferences(this);
Editor edit = prefs.edit();
edit.putString(konto_name1, name)
edit.commit();

或者,如果您希望它作为单线:

PreferenceManager.getDefaultSharedPreferences(this).edit().putString(konto_name1, name).commit();

然后不要从 Intent 中获取字符串,只需使用:

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String newString = prefs.getString(konto_name1, "");

这会做你想做的事,而不必跟踪从 Intent 到 Intent 传递的字符串,我曾经觉得这充其量是烦人的。

再次,上面作为单线:

PreferenceManager.getDefaultSharedPreferences(this).getString(konto_name1, "");

您应该对 Activity2 中的字符串执行相同的操作。不用担心,每次在 Activity(1/2) 中单击您的项目时,首选项都会被覆盖。请注意,第二个参数getString()是默认字符串,以防您要检索的字符串不存在。

于 2013-08-12T14:10:31.100 回答
1

像这样修改您的第二个活动代码。在第二个活动中看到你有不同的意图。它与第一个活动的意图无关。因此,您需要将值从第一个活动意图转移到第二个活动意图。

Intent intent = new Intent (this, Activity3.class);



EditText et2 = (EditText) findViewById(R.id.editText2);

String value = et2.getText().toString();

if (value.length() == 0) {
    new AlertDialog.Builder(this).setMessage(
            R.string.error_value_missing).setNeutralButton(
            R.string.error_ok,
            null).show();
    return;
}

intent.putExtra(start_value1, value);

intent.putExtra(konto_name1,getIntent().getStringExtra(konto_name1));//Add this line in your code
startActivity(intent);


}
于 2013-08-12T13:01:23.597 回答