当我的两个EditText不为空时,我可以更改Button描边颜色吗?

时间:2015-11-02 18:09:06

标签: java android

当两个Button不为空时,如何更改EditText的笔触颜色?

代码:

if (editText1.getText().toString().trim().length() > 0 && editText2.getText().toString().trim().length() > 0) {
    btn.setBackgroundResources(R.drawable.change_stroke);
} else {
    btn.setBackgroundResources(R.drawable.change_normal);
}

2 个答案:

答案 0 :(得分:0)

使用文本观察器。 onTextChanged在那里做你的魔法。

private class MyTextWatcher implements TextWatcher {

    private View view;

    private MyTextWatcher(View view) {
        this.view = view;
    }

    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        //set the default button stroke color

    }

    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            //set the final button stroke color

        }
      }
  }

使用可绘制文件或资源文件

定义颜色

答案 1 :(得分:0)

您应为每个TextWatcher实施并注册EditText

使用一些代码:

    // lookup for Views
    final EditText e1 = (EditText) rootView.findViewById(R.id.editText);
    final EditText e2 = (EditText) rootView.findViewById(R.id.editText2);
    final Button btn = (Button) rootView.findViewById(R.id.button);

    // TextWatcher definitions (to react on text changes)
    TextWatcher tw = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) { }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (e1.getText().length() > 0 && e2.getText().length() > 0) {
                btn.setBackgroundResources(R.drawable.change_stroke);
            } else {
                btn.setBackgroundResources(R.drawable.change_normal);
            }
        }

        @Override
        public void afterTextChanged(Editable s) { }
    }

    // text watcher registration to the 2 EditText
    e1.addTextChangedListener(tw);
    e2.addTextChangedListener(tw);