【问题标题】:Not able to change color of EditText background color dynamically无法动态更改 EditText 背景颜色的颜色
【发布时间】:2014-10-14 07:14:34
【问题描述】:

我在我的应用程序中使用 Edittexts。但无法在运行时设置它们的背景颜色。如果我做错了什么,请提出建议。

我已经采用了三个编辑文本,它们应该根据 do while 循环中使用的 if 编码改变它们的颜色。请检查并尽快回复。提前致谢。

这是我的代码。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



            List<Map<String,String>> list = new ArrayList<Map<String,String >>();
            EditText ed1 = (EditText) findViewById(R.id.name);
            EditText ed2 = (EditText) findViewById(R.id.age);
            EditText ed3 = (EditText) findViewById(R.id.date);
            Map<String,String> map = new HashMap<String, String>();

            int i = 2;
            EmpDatabase empClick = new EmpDatabase(getApplicationContext());
            Cursor cursor = empClick.getDetails();
            if(cursor.moveToFirst()){
                do{
                    i= i+1;
                    if(i % 2 == 0)
                    {
                        // here is something wrong
                        ed1.setBackgroundColor(Color.BLUE);
                        ed2.setBackgroundColor(Color.BLUE);
                        ed3.setBackgroundColor(Color.BLUE);

                    }
                    else 
                    {
                        // here is something wrong
                        ed1.setBackgroundColor(Color.CYAN);
                        ed2.setBackgroundColor(Color.CYAN);
                        ed3.setBackgroundColor(Color.CYAN);
                    }
                    map = new HashMap<String, String>();
                    String name = cursor.getString(cursor.getColumnIndex("name"));
                    String age = cursor.getString(cursor.getColumnIndex("age"));
                    String time = cursor.getString(cursor.getColumnIndex("time"));
                    map.put("name",name);
                    map.put("age",age);
                    map.put("time", time);
                    list.add(map);

                }while(cursor.moveToNext());
                cursor.close();
            }


            SimpleAdapter adapter = new SimpleAdapter(this, list, R.layout.text_view, new String [] {"name", "age", "time"}, new int[] {R.id.name,R.id.age, R.id.date});
            setListAdapter(adapter);

    }

【问题讨论】:

  • hii 只使用颜色的 setbackground 和 setcontent 视图
  • 检查光标移动到第一个。如果条件不满足,可能是这样。
  • 没有光标问题..我取了一个整数i来改变颜色。
  • 我也试过 setbackground .. 但没用。
  • 试试这个int j = i%2;并使用j.equal("0");

标签: android android-edittext background-color


【解决方案1】:

你对 setContentView(YOUR_LAYOUT) 的调用在哪里,基本上没有这个你的布局甚至还没有生成,所以 findViewById(R.id.name) 将一无所获。

【讨论】:

  • 我认为这只是一个复制粘贴错误,如果 OP 忘记了 setContentView,他将什么也看不到。但他形容它就像没有颜色变化一样。
  • 没有布局问题..除此之外它工作正常,如果我删除 if else 条件..但不知道为什么这个背景颜色没有改变。
  • 我看到的唯一可能的原因是光标为空。
【解决方案2】:

我得到了这个问题的答案。 我做了一个扩展简单适配器的适配器类..并编写以下代码..

public class EmpCustomAdapter extends SimpleAdapter{

    private int[] colors = new int[] { 0x30FF0000, 0x300000FF };

    public EmpCustomAdapter(Context context, List<Map<String, String>> list, int resource, String[] from, int[] to) {
        super(context, list, resource, from, to);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = super.getView(position, convertView, parent);
        int colorPos = position % colors.length;
        view.setBackgroundColor(colors[colorPos]);
        return view;
    }
}

并使其对象和设置适配器..

EmpCustomAdapter adapter = new EmpCustomAdapter(this, list, R.layout.text_view, new String [] {"name", "age", "time"}, new int[] {R.id.name,R.id.age, R.id.date});
            setListAdapter(adapter);

它成功了.. :) 感谢所有帮助我摆脱这个问题的人..:)由于声誉问题而无法上传结果图片。:(

【讨论】:

    猜你喜欢
    • 2014-11-08
    • 2014-04-05
    • 2014-06-06
    • 2013-09-03
    • 2013-03-14
    • 2016-10-04
    • 2014-09-25
    • 1970-01-01
    相关资源
    最近更新 更多