[英]how I can change the color of the TextView while the user is typing the text in the EditText
請告訴我如何在用戶在同一片段的EditText
中鍵入文本時更改TextView
的顏色。 這是我的片段中的一些代碼。
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;
}
在您的代碼if (text2.hasFocus())
僅執行一次,並且在用戶未輸入EditText
時執行。
你必須使用監聽器來做到這一點。
例如:
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));
}
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.