Android的Eclipse的findViewByID以编程的CheckBox [英] Android Eclipse findViewByID with programmatically CheckBox

查看:218
本文介绍了Android的Eclipse的findViewByID以编程的CheckBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我编程方式创建少数几个复选框是这样的:

I create a few CheckBoxes programatically like this:

    public int cb_id = 1000;

    public void create_cb()
    {

            CheckBox cb1 = new CheckBox(this);
            cb1.setText("My CheckBox");
            cb1.setId(cb_id);

            LinearLayout ll_checkbox = (LinearLayout) findViewById(R.id.ll_checkbox);

            ll_checkbox.addView(cb1);

    }

这工作的对我很好,但我无法找到该ID的复选框...

This work's fine for me, but I can't find the CheckBox with the ID...

    public void find_cb()
    {

            CheckBox cb1 = (CheckBox) findViewById(cb_id);

            String content = cb1.getText().toString();

    }

这是行不通的,应用程序正在关闭。

This is not working, the app is closing.

推荐答案

使用以下code:

public void find_cb()
{
     LinearLayout ll_checkbox = (LinearLayout) findViewById(R.id.ll_checkbox);
     CheckBox cb1 = (CheckBox) ll_checkbox.findViewById(cb_id);
     String content = cb1.getText().toString();
}

这篇关于Android的Eclipse的findViewByID以编程的CheckBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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