如何在特定的Android片段中引用某个TextEdit?

时间:2022-10-19 23:48:07

I'm developing an Android app and for this I want to have a fragment with which the user can insert an amount. The fragment has a couple methods, like inserting the correct currency symbol on the basis of the country.

我正在开发一个Android应用程序,为此我想要一个用户可以插入金额的片段。该片段有几种方法,例如在国家/地区的基础上插入正确的货币符号。

I now want the user to be able to insert several amounts on the same screen. As far as I understand I can reuse one fragment several times for this. Every time the fragment is used in the main xml it gets an id, and every fragment contains a couple EditTexts which each have an id as well.

我现在希望用户能够在同一屏幕上插入多个金额。据我所知,我可以多次重复使用一个片段。每次在主xml中使用片段时,它都会获得一个id,每个片段都包含一对EditTexts,每个EditTexts都有一个id。

I now wonder how I can get the value of a certain EditText within a certain fragment. So lets say I want to get the result of edit_text_2 from within fragment_3 (both are their respective id's). How would I do this?

我现在想知道如何在某个片段中获取某个EditText的值。所以我想说我想从fragment_3中获取edit_text_2的结果(两者都是它们各自的id)。我该怎么办?

2 个解决方案

#1


0  

You can get result from fragment via callback. Please check following documents:

您可以通过回调从片段中获取结果。请检查以下文件:

http://developer.android.com/training/basics/fragments/communicating.html

#2


0  

Check these links out:

检查这些链接:

http://developer.android.com/training/basics/fragments/communicating.html http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

Fragments are made to be self-contained, so that they don't know what is outside of them, and their containing activity doesn't necessarily know what is inside of them. You need to create an interface for the Activity to call to get the reference to your EditText.

碎片是自包含的,因此它们不知道它们之外的东西,它们的包含活动不一定知道它们内部是什么。您需要为要调用的Activity创建一个接口以获取对EditText的引用。

#1


0  

You can get result from fragment via callback. Please check following documents:

您可以通过回调从片段中获取结果。请检查以下文件:

http://developer.android.com/training/basics/fragments/communicating.html

#2


0  

Check these links out:

检查这些链接:

http://developer.android.com/training/basics/fragments/communicating.html http://developer.android.com/guide/components/fragments.html#CommunicatingWithActivity

Fragments are made to be self-contained, so that they don't know what is outside of them, and their containing activity doesn't necessarily know what is inside of them. You need to create an interface for the Activity to call to get the reference to your EditText.

碎片是自包含的,因此它们不知道它们之外的东西,它们的包含活动不一定知道它们内部是什么。您需要为要调用的Activity创建一个接口以获取对EditText的引用。