【问题标题】:Reset EditText's background after custom background自定义背景后重置 EditText 的背景
【发布时间】:2012-02-12 12:31:04
【问题描述】:

在赋予 EditText 小部件自定义颜色后,如何重置其默认背景?

【问题讨论】:

    标签: android colors background android-edittext reset


    【解决方案1】:

    我必须为我编写的应用程序这样做。在此示例中,我检查 EditText 是否为空,如果为空,则创建一个 TextWatcher。看看吧:

    // highlight any unfilled boxes
    final EditText text = (EditText) activity.findViewById(id);
    String data = text.getText().toString();
    if (data.length() == 0) //
    {
        final Drawable oldBackground = activity.findViewById(id).getBackground();
        text.setBackgroundColor(Color.YELLOW);
        text.addTextChangedListener(new TextWatcher() //
        {
            @Override
            public void afterTextChanged(Editable s) //
            {
                text.setBackgroundDrawable(oldBackground);
            }
    
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) 
            {    }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {    }
        });
    }
    

    希望这会有所帮助!

    【讨论】:

    • 谢谢你,完成了这项工作。我在编辑之前使用 getBackground 获取参考,然后将背景设置为要重置的参考值。
    • @StefanDunn 太棒了!很高兴我能帮上忙。
    猜你喜欢
    • 2017-03-23
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 2011-10-30
    • 2010-10-16
    • 2018-07-13
    • 1970-01-01
    相关资源
    最近更新 更多