0

嗨我做了一个程序,当我在我的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);



        }

    });
4

3 回答 3

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);

            }
于 2013-11-12T04:58:02.267 回答
0

添加随机背景可绘制颜色:

Random rnd = new Random(); 
int color = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); 
View.setBackgoundDrawable(new ColorDrawable(color)));
于 2013-11-12T04:55:20.717 回答
0

你给随机颜色函数并从函数返回一种颜色......

Android:点击时生成随机颜色?

试试这个。

于 2013-11-12T04:52:40.020 回答