如何设置动态创建的Textview的背景颜色?

时间:2019-06-27 09:46:05

标签: android textview

我在android中创建动态textview。视图已创建,但是当我单击textview时,我想在android中设置该特定textview的背景色。我已经尝试过,但是它将颜色设置为仅上一个textview而不是选定的textview。

这是我的代码

public class Demo extends AppCompatActivity implements View.OnClickListener {
    private LinearLayout llOptions;
    private TextView textView, tvOptionFirstQue;
    private ArrayList<String> arrayList = new ArrayList<>();
    private static final int MY_BUTTON = 9000;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_quiz_demo);
        llOptions = findViewById(R.id.llOptions);
        tvOptionFirstQue = findViewById(R.id.tvOptionFirstQue);

        for (int i = 0; i < 4; i++) {
            textView = new TextView(Demo
                    .this);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

            float left = getResources().getDimension(R.dimen.common_twentyfive);
            float top = getResources().getDimension(R.dimen.common_five);
            float toppadding = getResources().getDimension(R.dimen.common_twelve);
            float right = getResources().getDimension(R.dimen.common_twentyfive);
            params.setMargins((int) left, (int) top, (int) right, 0);
            textView.setPadding(0, (int) toppadding, 0, (int) toppadding);
            textView.setTextSize(14);
            textView.setBackgroundResource(R.drawable.button_background_light_gray);
            textView.setGravity(Gravity.CENTER);
            textView.setTextColor(Color.BLACK);
            textView.setId(i);
            textView.setLayoutParams(params);
            textView.setText("test" + i);
            textView.setOnClickListener(this);
            arrayList.add(textView.getText().toString());
            this.llOptions.addView(textView);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case 0:
                View child = llOptions.getChildAt(v.getId());
                TextView textView1 = (TextView) child;
                onFirstOptionClisk(textView1);


        }
    }

    private void onFirstOptionClisk(TextView textView) {
        textView.setBackgroundResource(R.drawable.rounded_lightblue_answer_backgroun);
        textView.setTextColor(getResources().getColor(R.color.white));
    }

3 个答案:

答案 0 :(得分:1)

出现问题的原因是,您正在将类变量TextView创建为textView,并且每次在外观中仅使用新的引用进行更新,因此只有最后一个textView保留。这是一种逻辑问题。

现在的解决方案是在for循环中创建TextView的新对象,以便将不同的对象引用添加到LinearLayout llOptions中。您唯一需要做的就是每次在for循环中删除textView的类变量并创建对象:

public class Demo extends AppCompatActivity implements View.OnClickListener {
private LinearLayout llOptions;
private TextView tvOptionFirstQue; //Comment by Hari: textView removed from here.
private ArrayList<String> arrayList = new ArrayList<>();
private static final int MY_BUTTON = 9000;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fragment_quiz_demo);
    llOptions = findViewById(R.id.llOptions);
    tvOptionFirstQue = findViewById(R.id.tvOptionFirstQue);

    for (int i = 0; i < 4; i++) {
        TextView textView = new TextView(Demo
                .this); //Comment by Hari: creating TextView object each time so that new reference will be created each time instead of over writing the same reference each time.
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);

        //Rest are unchanged......

希望它将解决您的问题。让我知道您的经历。

答案 1 :(得分:0)

尝试一下-

textView.setBackgroundResource(getResources.getColor(R.color.black));

然后删除-

 textView.setBackgroundResource(R.drawable.button_background_light_gray)

答案 2 :(得分:0)

使用

textView.setBackgroundColor(Color.parseColor("#00ff00"));

代替

textView.setBackgroundResource(R.drawable.button_background_light_gray)

可以工作