搜索以编程方式创建的 Textview

发布于 2024-10-20 19:22:33 字数 673 浏览 1 评论 0原文

我使用 for 循环以编程方式添加 TextViews,并使用 setId(Int int) 为该特定 Textview 设置唯一 id。 但现在我想根据该 id 搜索 textView。 我怎样才能搜索?

发生错误“应用程序意外停止...”这是我的代码。

public class Idtest extends Activity {
    TextView tv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout ll=new LinearLayout(this);
        tv=new TextView(this);

        tv.setText("Hello");
        ll.addView(tv);

        tv=new TextView(this);
        tv=(TextView)ll.getChildAt(1);

        setContentView(tv);


    }
}

I'm adding TextViews programmatic using a for-loop and use setId(Int int) to set unique id for that perticular Textview.
But now I want to search textView on the basis of that id.
How can I search?

erorr occurd 'app stopped unexpectedly...' Here is my code.

public class Idtest extends Activity {
    TextView tv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout ll=new LinearLayout(this);
        tv=new TextView(this);

        tv.setText("Hello");
        ll.addView(tv);

        tv=new TextView(this);
        tv=(TextView)ll.getChildAt(1);

        setContentView(tv);


    }
}

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

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

发布评论

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

评论(3

许仙没带伞 2024-10-27 19:22:33

tv 显然在末尾为 nullLinearLayout 只有一个子级,位于索引 0 处),因此,您基本上是在调用 setContentView(null) ,这会导致异常。我不清楚你想要做什么(你的代码非常混乱)。

假设您尝试在 LinearLayout 中显示多个 TextView,这是我的建议:

public class Idtest extends Activity {
    LinearLayout mainLayout;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mainLayout = new LinearLayout(this);
        setContentView(mainLayout);

        for (int i=0; i < 10; i++) {
            TextView tv = new TextView(this);
            tv.setText("Hello " + i);
            mainLayout.addView(tv);
        }
    }
}

如果在以后的任何时候,您需要其中一个 TextView >s, do:

TextView tvX = mainLayout.getChildAt(X); // where X is between 0 and 9

另外,请注意,从代码创建布局是邪恶的。如果你能避免它,请这样做。例如,如果 TextView 的数量是动态的,那么从代码创建它们是完全正常的(尽管您可以增加它们)。但是,不建议同时从代码创建 LinearLayout。您应该将其包含在 XML 中。如果您也可以在 XML 中包含 TextView,那就更好了。

tv is obviously null at the end (the LinearLayout only has one child, which is at index 0), so you're basically calling setContentView(null) which results in an exception. It's not clear for me what you're trying to do (your code is pretty messed up).

Supposing you are trying to show multiple TextViews in a LinearLayout, here's my suggestion:

public class Idtest extends Activity {
    LinearLayout mainLayout;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mainLayout = new LinearLayout(this);
        setContentView(mainLayout);

        for (int i=0; i < 10; i++) {
            TextView tv = new TextView(this);
            tv.setText("Hello " + i);
            mainLayout.addView(tv);
        }
    }
}

If, at any later point, you need one of the TextViews, do:

TextView tvX = mainLayout.getChildAt(X); // where X is between 0 and 9

Also, please note that creating layout from code is evil. If you can avoid it, please do. For example, if the number of TextViews is dynamic, then it's perfectly normal to create them from code (although you could inflate them). However, it's not advisable to also create the LinearLayout from code. You should have that in an XML. If it's possible for you to also have the TextViews in an XML, that would be even better.

快乐很简单 2024-10-27 19:22:33

还有另一种选择...我们有 setTag()getTag() 方法。添加文本视图时为其指定一个标签,每当您想要进行搜索时,请使用 getTag()。

There is another option too...we have setTag() and getTag() methods. While adding a textview give a tag for it and whenever u want to do a search use getTag().

◇流星雨 2024-10-27 19:22:33

我想您正在将 TextView 控件添加到某些 ViewGroup (例如 LInearLayout)?

您可以使用 getChildCount() / getChildAt(index) 迭代 ViewGroup 子视图,并将子视图 ID 与您要搜索的视图 ID 进行比较。

I suppose you are adding your TextView controls to some ViewGroup (e.g. LInearLayout)?

You can iterate through the ViewGroup child views using getChildCount() / getChildAt(index) and compare the child view id with the one you're searching for.

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