【问题标题】:Intent.putExtra - How to send a string to the next activity?Intent.putExtra - 如何将字符串发送到下一个活动?
【发布时间】:2014-01-22 03:13:26
【问题描述】:

好的,我从用户输入的一些数据中创建了一个简单的字符串。我现在希望将此数据作为字符串(类似 CSV 的字符串)传递给下一个活动。在一个例子中,我看到了这个:

intent.putExtra(EXTRA_MESSAGE, string_to_be_sent)

但这会引发一个错误,说 EXTRA_MESSAGE 不是一个值 - 即。它被意图识别。我认为这个例子已经过时了。我应该用什么来发送字符串?我查看了文档并发现了这一点:

public Intent putExtra (String name, String value)

但是我的值的名称是什么?我选择什么,还是必须是包名?? 感谢您的帮助:)

【问题讨论】:

  • 你确实需要做一些基本的编程教程。 EXTRA_MESSAGE 是您尚未在任何地方定义的变量。对于大写和下划线的用户来说,按照惯例,它是一个常量,所以我希望看到类似final string EXTRA_MESSAGE = "Name of string extra"; 的东西,这是一个很好的例子,说明为什么“复制粘贴编程”不是一个成功的策略。没有已知的学习捷径。在提出基本问题之前,您还应该养成阅读文档的习惯。阅读putExtra 的文档会告诉你你需要知道的一切。
  • EXTRA_MESSAGE 只是一个唯一字符串标签,您应该使用它从第二个活动中检索数据
  • 我有。这是基于一个我认为已经过时的例子。 EXTRA_MESSAGE 是该方法识别的某种形式的 int。
  • 从那个页面 => In order for the next activity to query the extra data, you should define the key for your intent's extra using a public constant. So add the EXTRA_MESSAGE definition to the top of the MainActivity class: public class MainActivity extends Activity { public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; ... } 如果您阅读了文档,它会非常清楚地表明它是您想要用作额外密钥的任何值。我重复。这一切都清楚地记录在案。

标签: java android eclipse android-intent


【解决方案1】:

第一个活动:

Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("Variable Name",string_to_be_sent);
startActivity(intent);

第二次活动:

//Receiving data inside onCreate() method of Second Activity
String value = getIntent().getStringExtra("Variable Name");

【讨论】:

  • 是的,这行得通,其他人的答案也适用于未来的读者。谢谢!!
【解决方案2】:
Intent intent= new Intent(currentActivity.this, TheActivityYouWant to Start.class);
intent.putExtra("SOME_KEY", "Sending");
startActivity(intent);

为了在您的其他活动中检索此内容,请在您已开始的活动onCreate(Bundle savedInstanceState) 中这样做。

Bundle bundle = getIntent().getExtras();
if(bundle != null) {
String your_extra = bundle.getString("SOME_KEY");
}

【讨论】:

    【解决方案3】:
    intent.putExtra("anyNameYouWant", "desired string value");
    

    【讨论】:

    • 是的,这行得通。最后我不小心错过了 Extra ......哎呀,这就是我得到第二个错误的原因。谢谢!!
    • @SwedishArchitect 如果对您有帮助,您可以接受答案,谢谢。 :-)
    • 这很有帮助,并且最直接地解决了我的问题,但我认为上面的答案对未来的用户最有帮助。我通常会查看接受的答案以寻求其他人问题的帮助,因此认为这会将人们引导到正确的答案:)
    • 我永远不会原谅T.T
    【解决方案4】:
    String product = "hellow"; 
        Intent myintent = new Intent(getApplicationContext(),view1.class);
        myintent.putExtra("product", product);
    

    【讨论】:

    • 您还需要添加代码来检索您的数据 String product = myintent.getStringExtra("product");
    【解决方案5】:

    在发送活动中:

     String EXTRA_MESSAGE = "Your string to be sent.."
    
        intent.putExtra(EXTRA_MESSAGE, EXTRA_MESSAGE ); 
        startActivity(intent);
    

    然后在onCreate方法中接收Activity:

        Bundle bundle= getIntent().getExtras();
        String stringtoBeReceived = bundle.getString(EXTRA_MESSAGE);
    

    【讨论】:

      【解决方案6】:

      EXTRA_MESSAGE 当然意味着它是一个static final String 值。

      当其他人回答如何使用它时,我想补充一点,创建一个 Utils 类将帮助您更灵活地维护您的代码。即你可以创建一个Utils 类来保存你所有的常量值。

      final class Utils {
        private Utils(){}//avoid useless instantiation of the class
        private final static String EXTRA_MESSAGE = "extraMessage";
      }
      

      现在如果你想改变这个字符串的值,你只需要改变这个,而不是通过你所有的课程/活动并搜索你使用这个字符串值的地方。

      你可以这样做:

      myintent.putExtra(Utils.EXTRA_MESSAGE, "theMessage");
      String extra = bundle.getString(Utils.EXTRA_MESSAGE);
      

      现在如果你想改变EXTRA_MESSAGE的值,你只需要在Utils类中修改它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-09-24
        • 1970-01-01
        • 1970-01-01
        • 2013-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-28
        相关资源
        最近更新 更多