如何在两个活动之间传递字符串
我有两个活动 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要创建一个类来存储变量。在 ActivityB 中使用设置变量的值,创建的类存储它,并在 ActivityA 中获取变量的值。
创建一个类:GlobalVars.java。在这个类中放置:
公共类 GlobalVars 扩展了应用程序{
}
在 ActivityB 中将此行放入适当的位置:
在 ActivityA 中将此行放入适当的位置:
就是这样!
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.
Create a class: GlobalVars.java. In this class put this:
public class GlobalVars extends Application {
}
In ActivityB put this line in to the appropriate place:
In ActivityA put this line in to the appropriate place:
And that's it!
听起来您想在活动 B 返回结果时保留对话框。如果是这种情况,那么你可以打开对话框 onActivityResult:
注意: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:
Note: Activity A must not be SingleTask, SingleInstance, SingleTop.
也许尝试使用共享首选项!?
Perhaps try using sharedpreferences !?
您可以使用广播系统将包含数据的 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.
我认为你需要使用 Bundle 和静态全局变量以及 onActivityResult()。如果您想将以前的客户编辑为新客户。假设您有“ClientList”活动和“EditClient”活动
写入“EditClient”活动
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