3

我已成功togglebuttonactivity. 但我被困在处理setOnCheckedChangeListener每个togglebutton.

我创建了演示代码片段。

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

        for (int i = 0; i < 6; i++) {

            el = new LinearLayout(this);
            el.setOrientation(LinearLayout.HORIZONTAL);

            LinearLayout.LayoutParams emailLinearParams = new LinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

            el.setLayoutParams(emailLinearParams);
            el.setPadding(5, 5, 5, 5);

            LinearLayout.LayoutParams emailTxtParams = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 0.9f);

            TextView emailTxt = new TextView(this);
            emailTxt.setText("demo");
            emailTxt.setLayoutParams(emailTxtParams);
            emailTxt.setGravity(Gravity.CENTER_VERTICAL);

            el.addView(emailTxt);

            ToggleButton toggleEmailBtn = new ToggleButton(this);

            LinearLayout.LayoutParams emailToggleBtnParams = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.1f);

            toggleEmailBtn.setLayoutParams(emailToggleBtnParams);

            el.addView(toggleEmailBtn);

            linearLayout.addView(el);

        }
4

1 回答 1

0

尝试并解决了很好的答案:)

ToggleButton tglBtn[];

onCreate()

tglBtn = new ToggleButton[6];

   for (int i = 0; i < 6; i++) {

            el = new LinearLayout(this);
            el.setOrientation(LinearLayout.HORIZONTAL);

            LinearLayout.LayoutParams emailLinearParams = new LinearLayout.LayoutParams(
                    LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);

            el.setLayoutParams(emailLinearParams);
            el.setPadding(5, 5, 5, 5);

            LinearLayout.LayoutParams emailTxtParams = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT, 0.9f);

            TextView emailTxt = new TextView(this);
            emailTxt.setText("demo");
            emailTxt.setLayoutParams(emailTxtParams);
            emailTxt.setGravity(Gravity.CENTER_VERTICAL);

            el.addView(emailTxt);

            tglBtn[i] = new ToggleButton(this);

            LinearLayout.LayoutParams emailToggleBtnParams = new LinearLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0.1f);

            tglBtn[i].setLayoutParams(emailToggleBtnParams);

            el.addView(tglBtn[i]);

            linearLayout.addView(el);

            tglBtn[i]
                    .setOnCheckedChangeListener(handleOnClick(
                            tglBtn[i], i));

        }

使用方法onclick处理togglebuttons

CompoundButton.OnCheckedChangeListener handleOnClick(
            final ToggleButton button, final int id) {
        return new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton cbtn, boolean isCheck) {
                    Log.d(TAG, "clicked "+id);
            }
        };
    }
于 2013-04-14T11:35:03.140 回答