这是我的代码,我希望创建的切换按钮将创建的搜索栏的位置设置为 100/0,但是只有创建的最后一个切换按钮才能设置最后一个搜索栏的位置。我已经创建了按钮和搜索栏,并使用 for 循环为它们分配了 ID。谢谢您的帮助。
public class MainActivity extends Activity {
TableLayout tl = null;
SeekBar seekbar = null;
TextView t = null;
ToggleButton bt = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tl = (TableLayout)findViewById(R.id.lightslist);
t = (TextView)findViewById(R.id.progress);
for(int i = 1; i <= 20; i++){
TableRow tr = new TableRow(this);
seekbar = new SeekBar(this);
seekbar.setMax(100);
seekbar.setId(i);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar sb) {
// TODO Auto-generated method stub
int progress = sb.getProgress();
t.setText(sb.getId() + " at " + progress);
}
public void onStartTrackingTouch(SeekBar sb) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(), "Start", Toast.LENGTH_SHORT).show();
}
public void onProgressChanged(SeekBar sb, int progress, boolean a) {
// TODO Auto-generated method stub
}
});
TableRow.LayoutParams ob = new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
1);
bt = new ToggleButton(this);
bt.setId(i + 100);
bt.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
if (bt.isChecked())
{
seekbar.setProgress(100);
}else{
seekbar.setProgress(0);
}
}
});
tr.addView(bt);
tr.addView(seekbar, ob);
tl.addView(tr);
}
}