从一项活动到另一堂课获得价值 [英] Getting value from one activity to another class

查看:92
本文介绍了从一项活动到另一堂课获得价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以引导我朝正确的方向前进,因为这种情况是 有两个带有Activity的calssA类,而classB是简单的Java类

can anyone guide me to the right direction, as the situation is have two classes calssA with Activity while classB is simple java class

1.classA有一个编辑框,用户可以在其中输入一些值. 2.classB有一种计算用户输入的方法.

1.classA has a editbox where the user inputs some value. 2.classB has a method that computes the user input.

需要将用户输入值从classA转移到classB.

need to get the user input value from classA to classB.

a).因为另一个类没有活动,所以无法通过传递意图来获得它. b).尝试通过创建类来尝试使用getter setter方法,从而在用户输入详细信息的地方调用setter. 并在另一个类中调用getter.

a).cannot get it through passing intent as the other class does not have activity. b).have tried getter setter method by creating a class thus,calling setter where the user inputs the detail. and calling the getter in another class.

该值仍然为null,因此这里显然缺少什么.对示例或简要说明的任何指导都将是很棒的.谢谢

Still the value is null, so what apparently is missing here.Any guidance to a example or brief explanation would be great.Thanks

推荐答案

您可以将edittext值保存在SharedPreferences中,从而使其在所有活动中都可用. 通过这样做,您可以轻松地在其他活动中获取价值.

You can save the edittext value in SharedPreferences, thus making it available in all activity. By doing this you can fetch the value in other activity without any hassle.

例如:

SharedPreferences.Editor editor = preferences.edit();
editor.putString("edtTextValue", valueOfEditText);
editor.commit();

进一步获取其他活动中的值,

Further fetching the value in other activity like this,

preferences.getString("edtTextValue", "");

这篇关于从一项活动到另一堂课获得价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆