我做了一个程序,当我在editText中输入数字时,它将自动显示包含edittexts等于我输入的数字的tablerow,我使用for循环来执行此操作,但我希望在每个循环中它将改变编辑文本的背景颜色我将如何做到这一点,提前谢谢......
这是我的代码......
final EditText et = (EditText)findViewById(R.id.questions);
Button button1 = (Button)findViewById(R.id.btn);
button1.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String c = et.getText().toString();
Integer count = Integer.parseInt(c);
TableLayout table = new TableLayout(getApplicationContext());
table.setVerticalScrollBarEnabled(true);
table.setPadding(10, 10, 10, 10);
TableRow tableRow = new TableRow (getApplicationContext());
TextView txt = new TextView (getApplicationContext());
TextView txt2 = new TextView (getApplicationContext());
TextView txt3 = new TextView (getApplicationContext());
TextView txt4 = new TextView (getApplicationContext());
TextView txt5 = new TextView (getApplicationContext());
TextView txt6 = new TextView (getApplicationContext());
tableRow.addView(txt);
tableRow.addView(txt2);
tableRow.addView(txt3);
tableRow.addView(txt4);
tableRow.addView(txt5);
tableRow.addView(txt6);
txt.setText("Question ");
txt2.setText("Excellent ");
txt3.setText("Best ");
txt4.setText("Better ");
txt5.setText("Good ");
txt6.setText("Poor ");
table.addView(tableRow);
int j=0;
for(j = 1; j<=count; j++){
tableRow = new TableRow (getApplicationContext());
TextView name = new TextView (getApplicationContext());
EditText et2 = new EditText (getApplicationContext());
EditText et3 = new EditText (getApplicationContext());
EditText et4 = new EditText (getApplicationContext());
EditText et5 = new EditText (getApplicationContext());
EditText et6 = new EditText (getApplicationContext());
et2.setBackgroundColor(Color.WHITE);
et3.setBackgroundColor(Color.WHITE);
et4.setBackgroundColor(Color.WHITE);
et5.setBackgroundColor(Color.WHITE);
et6.setBackgroundColor(Color.WHITE);
tableRow.addView(name);
tableRow.addView(et2);
tableRow.addView(et3);
tableRow.addView(et4);
tableRow.addView(et5);
tableRow.addView(et6);
table.addView(tableRow);
}
setContentView(table);
}
});
答案 0 :(得分:1)
试试这个
for(j = 1; j<=count; j++){
tableRow = new TableRow (getApplicationContext());
TextView name = new TextView (getApplicationContext());
EditText et2 = new EditText (getApplicationContext());
EditText et3 = new EditText (getApplicationContext());
EditText et4 = new EditText (getApplicationContext());
EditText et5 = new EditText (getApplicationContext());
EditText et6 = new EditText (getApplicationContext());
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
et2.setBackgroundColor(color);
et3.setBackgroundColor(color);
et4.setBackgroundColor(color);
et5.setBackgroundColor(color);
et6.setBackgroundColor(color);
tableRow.addView(name);
tableRow.addView(et2);
tableRow.addView(et3);
tableRow.addView(et4);
tableRow.addView(et5);
tableRow.addView(et6);
table.addView(tableRow);
}
答案 1 :(得分:0)
答案 2 :(得分:0)
添加随机背景可绘制颜色:
Random rnd = new Random();
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
View.setBackgoundDrawable(new ColorDrawable(color)));