如果我在单击按钮时在同一父级中多次膨胀视图,我如何知道视图的 ID?

发布于 2024-12-21 05:06:00 字数 272 浏览 0 评论 0原文

我遇到了一个困难的情况,让我这样说,我希望用户使用我的 3 个文本字段来设置警报数据,填写信息后,我给他一个选项来设置另一个警报,我使用一个按钮。我在表布局中有三个表行,它们在 child.xml 文件中定义,并且每个元素都有一个 Id 集。在我的活动中,我在按钮单击事件上多次膨胀 child.xml,并且我能够根据需要成功多次膨胀它。但我的问题是,因为我在运行时多次(我不知道多少次)膨胀同一个 xml 文件,所以我很困惑如何从特定文本字段(如 TextViews)检索数据并通过唯一的 ID,因为我需要这样做。 你怎样做呢?

I have a difficult situation, let me put it this way, I want the user to use my 3 text-fields to set data for alarm and after filling in the information, I give him an option to set another alarm for this purpose I use a button. I have three table rows in a table layout which are defined in a child.xml file and every element has an Id set. In my activity, I inflate child.xml multiple times on button click event and I am able to successfully inflate it as many times as I want. But my problem is since I am inflating the same xml file many times(I don't know how many times) at runtime, I am confused as of how would I retrieve the data from particular text-fields like TextViews and identify them by a unique IDs since I am required to do so.
How do you go about it?

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

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

发布评论

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

评论(4

深府石板幽径 2024-12-28 05:06:00

我会以编程方式解决这个问题。

当用户请求新警报时,您会膨胀 xml 文件。此时,在您的活动中保留对膨胀视图的引用(在列表或某种其他类型的集合中)。

因此,当您想要检索数据时,不要使用 findViewById,而只需使用您的引用即可。

I would go programmatically about it.

When the user requests for a new alarm, then you inflate your xml file. At this point, keep a reference to the inflated view (in a List, or some other sort of collection) in your activity.

So when you want to retrieve the data, don't use findViewById, just use your reference instead.

也只是曾经 2024-12-28 05:06:00

也许这可以帮助...


            LinearLayout ll=(LinearLayout) findViewById(R.id.i1);   ///main xml file which contains the layout i m inflating
            String str[]={"a","b","c"};
            View vi[]=new View[3];
            LayoutInflater li=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
            for(int i=0;i<3;i++){
                  View v=li.inflate(R.layout.addit, null);
                  TextView t=(TextView) v.findViewById(R.id.t1);
                  t.setText(str[i]);
                  ll.addView(v);
                      vi[i]=v;
            }
            TextView t1=new TextView(this);
            TextView t2=new TextView(this);
            TextView t3=new TextView(this);
            TextView r1=(TextView) vi[0].findViewById(R.id.t1);
            TextView r2=(TextView) vi[1].findViewById(R.id.t1);
            TextView r3=(TextView) vi[2].findViewById(R.id.t1);
            t1.setText(r1.getText());
            t2.setText(r2.getText());
            t3.setText(r3.getText());
            ll.addView(t1);
            ll.addView(t2);
            ll.addView(t3);

             setContentView(ll);

我的 addit.xml 文件包含以下内容:

       <?xml version="1.0" encoding="utf-8"?>
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/l2"
          android:layout_width="match_parent"
          android:layout_height="100dp"
          android:orientation="vertical" >

       <TextView
          android:id="@+id/t1"
          android:layout_width="fill_parent"
          android:layout_height="40dp"
          android:text="" />
       </TextView>
       </LinearLayout>

这里我从 addit.xml 动态添加布局 3 次,其中包含 id t1 的文本视图..
对于每个文本视图,我从字符串数组 str ..
并将每个视图对象 v 添加到数组中以供引用......
现在,如果我想获取每个文本视图的文本,我可以使用对存储在数组中的视图的引用,并按照代码中的 id 查找文本视图,以从每个文本视图获取文本

在此处输入图像描述

may be this can help...


            LinearLayout ll=(LinearLayout) findViewById(R.id.i1);   ///main xml file which contains the layout i m inflating
            String str[]={"a","b","c"};
            View vi[]=new View[3];
            LayoutInflater li=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
            for(int i=0;i<3;i++){
                  View v=li.inflate(R.layout.addit, null);
                  TextView t=(TextView) v.findViewById(R.id.t1);
                  t.setText(str[i]);
                  ll.addView(v);
                      vi[i]=v;
            }
            TextView t1=new TextView(this);
            TextView t2=new TextView(this);
            TextView t3=new TextView(this);
            TextView r1=(TextView) vi[0].findViewById(R.id.t1);
            TextView r2=(TextView) vi[1].findViewById(R.id.t1);
            TextView r3=(TextView) vi[2].findViewById(R.id.t1);
            t1.setText(r1.getText());
            t2.setText(r2.getText());
            t3.setText(r3.getText());
            ll.addView(t1);
            ll.addView(t2);
            ll.addView(t3);

             setContentView(ll);

my addit.xml file contains the following :

       <?xml version="1.0" encoding="utf-8"?>
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/l2"
          android:layout_width="match_parent"
          android:layout_height="100dp"
          android:orientation="vertical" >

       <TextView
          android:id="@+id/t1"
          android:layout_width="fill_parent"
          android:layout_height="40dp"
          android:text="" />
       </TextView>
       </LinearLayout>

here i m adding dynamically the layout from addit.xml 3 times which contains the textview with id t1..
for each textview i m setting a text from the string array str ..
and adding each view object v to an array for referencing ...
now if i want to get the text for each textview i can use the reference to the view stored in the array and find the text view by id as in the code to get the text from each textview

enter image description here

痴情 2024-12-28 05:06:00

我将从基本的

<Layout>
<Button1>
</layout>

现在开始活动

Button btn;View view1=null;
///Inflate file if view is null
first time when you inflating
view1=inflate
 btn=view1.findViewbyid();

现在假设您已经膨胀了另一个视图,但 view1 值没有更改,那么 btn 将包含该按钮的引用。当它变得可见时。它将像普通按钮一样工作。

i will start from basic

<Layout>
<Button1>
</layout>

Now in activity

Button btn;View view1=null;
///Inflate file if view is null
first time when you inflating
view1=inflate
 btn=view1.findViewbyid();

Now suppose you have inflate another view but view1 value is not changed then btn willl contain reference for that button.And when it became visible.It will work as like normal button.

断爱 2024-12-28 05:06:00

只需记住在每个实例 inflate() 时返回的 View 引用即可。当您需要从中读取文本时,将它们投射到 TextView 上。

Just remember the View reference you get back when you inflate() each instance. Cast these to TextView when you need to read text from them.

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