【问题标题】:Intent putExtra from two Activities来自两个活动的 Intent putExtra
【发布时间】:2013-08-12 22:10:31
【问题描述】:

您好,希望您能帮到我:

我有三个活动: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);

    }   

来自 Activity2:

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);      
    }

【问题讨论】:

  • 使用 sharedpreference 或再次从 activity2 发送相同的值
  • 你能告诉我在活动2中获取konto_name1的代码吗
  • 你解决了吗?

标签: android


【解决方案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);


}

【讨论】:

    【解决方案2】:

    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);
    

    现在您可以使用 getIntent() 获取 Activity3.java 中的两个值。

    希望对你有帮助。

    【讨论】:

    • @JonasW。如果您解决了,请接受答案,以便对其他开发人员有所帮助。
    【解决方案3】:

    正如其他答案所讨论的那样,您可以从 Intent 传递到 Intent。不过,您也可以使用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() 中的第二个参数是默认字符串,以防您要检索的字符串不存在。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 1970-01-01
      相关资源
      最近更新 更多