【问题标题】:How to pass a value from one Activity to another in Android?如何在 Android 中将值从一个 Activity 传递到另一个?
【发布时间】:2010-08-18 09:30:25
【问题描述】:

我创建了一个带有 AutuCompleteTextView[ACTV] 和按钮的 Activity。我在 ACTV 中输入了一些文本,然后按下按钮。 按下按钮后,我希望 Activity 转到另一个 Activity。在第二个活动中,我只想将在 ACTV(第一个活动)中输入的文本显示为 TextView。

我知道如何开始第二个活动,如下所示:

Intent i = new Intent(this, ActivityTwo.class);
startActivity(i);

我已经对此进行了编码以获得从 ACTV 输入的文本。

AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
CharSequence getrec=textView.getText();

我的问题是如何将“getrec”(按下按钮后)从第一个 Activity 传递到第二个。然后在第二个活动中收到“getrec”。

请假设我已经使用“onClick(View v)”为按钮创建了事件处理程序类

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以在 Android 中使用 Bundle 来做同样的事情

    创建意图:

    Intent i = new Intent(this, ActivityTwo.class);
    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete);
    String getrec=textView.getText().toString();
    
    //Create the bundle
    Bundle bundle = new Bundle();
    
    //Add your data to bundle
    bundle.putString(“stuff”, getrec);
    
    //Add the bundle to the intent
    i.putExtras(bundle);
    
    //Fire that second activity
    startActivity(i);
    

    现在在您的第二个活动中从包中检索您的数据:

    //Get the bundle
    Bundle bundle = getIntent().getExtras();
    
    //Extract the data…
    String stuff = bundle.getString(“stuff”); 
    

    【讨论】:

      【解决方案2】:

      将数据从一个活动传递到另一个活动的标准方式:

      如果您想将大量数据从一个活动发送到另一个活动,那么您可以将数据放在一个包中,然后使用putExtra() 方法传递它。

      //Create the `intent`
       Intent i = new Intent(this, ActivityTwo.class);
      String one="xxxxxxxxxxxxxxx";
      String two="xxxxxxxxxxxxxxxxxxxxx";
      //Create the bundle
      Bundle bundle = new Bundle();
      //Add your data to bundle
      bundle.putString(“ONE”, one);
      bundle.putString(“TWO”, two);  
      //Add the bundle to the intent
      i.putExtras(bundle);
      //Fire that second activity
      startActivity(i);
      

      否则你可以直接使用putExtra()意图发送数据,使用getExtra()获取数据。

      Intent i=new Intent(this, ActivityTwo.class);
      i.putExtra("One",one);
      i.putExtra("Two",two);
      startActivity(i);
      

      【讨论】:

        【解决方案3】:

        这很简单,使用 Intent.putExtra 将数据传递给您开始的活动。 然后使用 Bundle.getExtra 来检索它。

        这样的问题已经很多了https://stackoverflow.com/search?q=How+to+pass+a+value+from+one+Activity+to+another+in+Android 下次请务必先使用搜索。

        【讨论】:

          【解决方案4】:

          这样实现

          String i="hi";
          Intent i = new Intent(this, ActivityTwo.class);
          //Create the bundle
          Bundle b = new Bundle();
          //Add your data to bundle
          b.putString(“stuff”, i);
          i.putExtras(b);
          startActivity(i);
          

          从第二个activity 开始,在这个class 中使用 Bundle 值使用这个代码

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

          【讨论】:

            【解决方案5】:

            如果您将字符串 X 从 A 传递到 B,这很简单。
            A--> B

            在活动 A 中
            1) 创建意图
            2) 使用意图的 putExtra 方法将数据放入意图中
            3) 开始活动

            Intent i = new Intent(A.this, B.class);
            i.putExtra("MY_kEY",X);
            

            在活动 B 中
            在 onCreate 方法内
            1) 获取意图对象
            2) 使用 key(MY_KEY) 获取存储值

            Intent intent = getIntent();
            String result = intent.getStringExtra("MY_KEY");
            

            这是从 A 向 B 发送数据的标准方式。 您可以发送任何数据类型,它可以是 int、boolean、ArrayList、String[]。根据您作为键存储在 Activity 中的数据类型,值对检索方法可能会有所不同,例如如果您传递的是 int 值,那么您将调用

            intent.getIntExtra("KEY");
            

            您甚至可以发送 Class 对象,但为此,您必须让您的类对象实现 Serializable 或 Parceable 接口。

            TransactionTooLargeException

            您可以跨大小发送多少数据。 如果数据大小超过一定数量,那么您可能会收到 TransactionTooLargeException。 假设您尝试通过活动发送位图,并且如果大小超过特定数据大小,那么您可能会看到此异常。

            【讨论】:

              【解决方案6】:

              在第一个活动中:

              Intent i=new Intent(getApplicationContext,secondActivity.class);
              
              i.putExtra("key",value);
              
              startActivity(i);
              

              在SecondActivity中:

              String value=getIntent.getStringExtra("Key");
              

              【讨论】:

                【解决方案7】:

                示例 Kotlin 代码如下:-

                第 1 页

                val i = Intent(this, Page2::class.java)
                            val getrec = list[position].promotion_id
                            val bundle = Bundle()
                            bundle.putString("stuff", getrec)
                            i.putExtras(bundle)
                            startActivity(i)
                

                第 2 页

                        var bundle = getIntent().getExtras()
                        var stuff = bundle.getString("stuff")
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2016-01-18
                  • 2017-04-05
                  • 2020-10-19
                  • 1970-01-01
                  • 2021-06-08
                  • 2015-10-25
                  • 1970-01-01
                  • 2018-12-25
                  相关资源
                  最近更新 更多