我已成功togglebutton
在activity
. 但我被困在处理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);
}