3

I have the following code:

nameOfInf.setOnFocusChangeListener(new OnFocusChangeListener() {
    if (strTollAmount.length() > 0) {
        nameOfInf.setBackgroundColor(getResources().getColor(android.R.color.white));
        nameOfInf.setTextColor(getResources().getColor(android.R.color.black));
    }
});
tollAmount.setOnFocusChangeListener(new OnFocusChangeListener() {
    if (strInfName.length() > 0) {
        tollAmount.setBackgroundColor(getResources().getColor(android.R.color.white));
        tollAmount.setTextColor(getResources().getColor(android.R.color.black));
    }
});

The function checks to see if the value on the textbox is anything other than empty or null. So if the user enters something in the text box the background and foreground color should change. But that's not happening. Any idea how to edit it?

4

4 回答 4

9

我认为您正在寻找的是 TextWatcher (尽管 onFocusChanged 可能有效,但这是另一种方法)。这是示例代码:

TextWatcher watcher= new TextWatcher() {
        public void afterTextChanged(Editable s) { 
            if (TextBox1.getText().toString().equals("")) {
                 TextBox1.setBackgroundColor(getResources().getColor(android.R.color.white));
                 TextBox1.setTextColor(getResources().getColor(android.R.color.black));
            }

        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
              //Do something or nothing.                
        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //Do something or nothing
        }
    };

    TextBox1.addTextChangedListener(watcher);

此外,您还需要使用.equals().

于 2013-07-24T13:57:00.817 回答
4
txt = (EditText) findViewById(R.id.txtChange);
    txt.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub
            if (TextUtils.isEmpty(s.toString().trim())) {
                txt.setBackgroundColor(Color.RED);
            } else
                txt.setBackgroundColor(Color.GREEN);
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    });
于 2013-07-24T13:58:31.420 回答
1

你可以你的TextWatcher,这是一个例子:

    final EditText et = new EditText(getApplicationContext());

TextWatcher tw = new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    if (s.length() > 0) {
        et.setBackgroundColor(Color.WHITE);
    } else {
        et.setBackgroundColor(Color.GRAY);
    }

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    // TODO Auto-generated method stub

    }

    @Override
    public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

    }
};

et.addTextChangedListener(tw);
setContentView(et);
于 2013-07-24T14:07:34.187 回答
0

你的方法是我认为绝对错误的(它编译?)。您需要测试字段的内容长度。我不确切知道您要在代码中做什么。

这里有两种可能的方法:

if (TextBox1.getText().toString().length() > 0) {
   // is not empty
}
else {
   // is empty
}

or

if (!TextUtils.isEmpty(TextBox1.getText().toString())) {
   // is not empty
}
else {
   // is empty
}
于 2013-07-24T13:56:13.260 回答