【问题标题】:Android Eclipse findViewByID with programmatically CheckBoxAndroid Eclipse findViewByID 以编程方式 CheckBox
【发布时间】:2014-02-25 04:41:09
【问题描述】:

我像这样以编程方式创建了几个 CheckBox:

    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 的 CheckBox...

    public void find_cb()
    {

            CheckBox cb1 = (CheckBox) findViewById(cb_id);

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

    }

这不起作用,应用程序正在关闭。

【问题讨论】:

    标签: java android eclipse checkbox findviewbyid


    【解决方案1】:

    使用以下代码:

    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();
    }
    

    【讨论】:

    • 我不确定我的答案@DigveshPatel,我只是在等待看到它可能对 OP 有用,我将删除它,因为我不确定它是否正常工作,
    • 亲爱的,您的答案是 ri8,但需要澄清一下,以便他/她了解您的答案 +1
    【解决方案2】:

    试试这个..

    使用

    CheckBox cb1;
    

    作为 全局 像 public int cb_id = 1000; 和内部 create_cb() 方法只需使用 cb1 = new CheckBox(this); 和 find_cb() 方法,如下所示

    public void find_cb()
    {
       String content = cb1.getText().toString();
    }
    

    【讨论】:

    • 问题是 CheckBox 的数量总是取决于用户。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-18
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    相关资源
    最近更新 更多