如何在两个活动之间传递字符串

发布于 2024-11-15 19:52:13 字数 146 浏览 3 评论 0原文

我有两个活动 A 和 B,从活动 A 中,我单击一个按钮,打开一个对话框,其中包含一个由两个编辑文本字段和一个按钮组成的表单(对话框中的按钮用于启动活动 B) 。所以,我的问题是:如何将字符串从活动 B 传递到活动 A,但不关闭对话框(该字符串将用于填充两个编辑文本字段之一)。

I have two activities A and B, and from activity A, I click on a button that opens a dialog box which contains a form consisting of two edit text fields and a button(the button in the dialog box is used to start activity B). So, my question is: how do I pass a string from activity B to activity A, but without closing the dialog box(the string will be used to fill one of the two edit text fields).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦与时光遇 2024-11-22 19:52:13

您需要创建一个类来存储变量。在 ActivityB 中使用设置变量的值,创建的类存储它,并在 ActivityA 中获取变量的值。

  1. 创建一个类:GlobalVars.java。在这个类中放置:

    公共类 GlobalVars 扩展了应用程序{

    私有静态字符串var2;
    
    公共静态字符串 getVar() {
        返回var2;
    }
    
    公共静态无效setVar(字符串变量){
    var2 = var;
    }
    

    }

在 ActivityB 中将此行放入适当的位置:

String something;
GlobalVars.setVar(something);

在 ActivityA 中将此行放入适当的位置:

String getsomething = GlobalVars.getVar();

就是这样!

You need to create a class to store the variable. In ActivityB use set the value of the variable, the created class stores it and in ActivityA get the value of the variable.

  1. Create a class: GlobalVars.java. In this class put this:

    public class GlobalVars extends Application {

    private static String var2;
    
    public static String getVar() {
        return var2;
    }
    
    public static void setVar(String var) {
    var2 = var;
    }
    

    }

In ActivityB put this line in to the appropriate place:

String something;
GlobalVars.setVar(something);

In ActivityA put this line in to the appropriate place:

String getsomething = GlobalVars.getVar();

And that's it!

佞臣 2024-11-22 19:52:13

听起来您想在活动 B 返回结果时保留对话框。如果是这种情况,那么你可以打开对话框 onActivityResult:

  1. Activity A
  2. 单击按钮打开对话框
  3. 启动 Activity B
  4. 将结果返回给 Activity A
  5. onActivityResult 将再次调用
  6. 打开对话框

注意:Activity A 不能是 SingleTask、SingleInstance、SingleTop。

It sound like you want to keep dialogbox when activity B returns result. If such case then you can open dialog box onActivityResult:

  1. Activity A
  2. Click on button open dialogbox
  3. start Activity B
  4. return result to Activity A
  5. onActivityResult will call
  6. open dialog box again

Note: Activity A must not be SingleTask, SingleInstance, SingleTop.

つ低調成傷 2024-11-22 19:52:13

也许尝试使用共享首选项!?

Perhaps try using sharedpreferences !?

陪我终i 2024-11-22 19:52:13

您可以使用广播系统将包含数据的 Intent 发送到另一个活动。

搜索 google 或 stackoverflow 有很多关于如何实现这一点的教程和示例。
据我了解,您希望活动 a 收到通知并根据对话框中的某些操作填充字段。

我建议的是这样做的一种方法。其他答案也为同一问题提供了不同的解决方案。您还可以在创建对话框时注册一个接口,该接口将从对话框内部调用并在第一个活动中执行某些操作。

You can use the broadcast system to send an Intent containing data to another activity.

Search google or stackoverflow there are a lot of tutorials and examples of how to achieve this.
as i understand you want activity a to get notified and fill a field based on some action in the dialog.

what i am suggesting is one way of doing this. the other answers provide also different solutions to the same problem. also you can register an interface with the creation of your dialog which will be called from inside the dialog and do something in the first activity.

不寐倦长更 2024-11-22 19:52:13

我认为你需要使用 Bundle 和静态全局变量以及 onActivityResult()。如果您想将以前的客户编辑为新客户。假设您有“ClientList”活动和“EditClient”活动

写入“EditClient”活动

Bundle extras = getIntent().getExtras();
  if (extras != null) 
  {
      String name = extras.getString(ClientList.KEY_Client);//ClientList.KEY_Client is global static variable of "ClientList" Activity.

      if (name != null) 
      {
          nameText.setText(name);//"nameText" is a EditText object represent EditText view
      }

  }

I think you need to use Bundle and static global variable and onActivityResult(). If you want to edit client with previous client to new client . Suppose you have "ClientList" Activity and "EditClient" Activity

Write into "EditClient" Activity

Bundle extras = getIntent().getExtras();
  if (extras != null) 
  {
      String name = extras.getString(ClientList.KEY_Client);//ClientList.KEY_Client is global static variable of "ClientList" Activity.

      if (name != null) 
      {
          nameText.setText(name);//"nameText" is a EditText object represent EditText view
      }

  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文