Please tell me how I can change the color of the TextView
while the user is typing the text in the EditText
in the same fragment. Here is some of the code from my fragment.
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_second, container, false);
TextView text1 = v.findViewById(R.id.textView1);
EditText text2 = v.findViewById(R.id.EditText2);
if (text2.hasFocus()) {text1.setTextColor(Color.BLACK);} else text1.setTextColor(Color.RED);
return v;
}
In your code if (text2.hasFocus())
is executed only once and when the user is not typing in the EditText
.
You have to use a listener to do that.
For example:
EditText editText = findViewById(R.id.edittext);
TextView textView = findViewById(R.id.textview);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
textView.setTextColor(ContextCompat.getColor(context, R.color.xxxx));
} else {
textView.setTextColor(ContextCompat.getColor(context, R.color.xxx));
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.