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 に答える