0
這是我的代碼,我希望創建的切換按鈕將創建的搜索欄的位置設置爲100/0,但只有創建的最後一個切換按鈕才能設置最後一個搜索欄的位置。我創建了按鈕和seekbar,並使用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);
}
}