2011-08-11 54 views
2

我有一個編輯文本字段。我希望它具有透明背景。但是當用戶試圖編輯它時,它應該有不同的背景,說白色和文字顏色爲黑色。當他完成編輯文本的編輯並移動到下一個控件以輸入其他值時,我希望背景再次變爲透明。請讓我知道這是如何在Android中可能的。感謝您的幫助和時間。如何在編輯模式下控制編輯文本的背景。對於Android?

回答

3
EditText editText = (EditText)findViewById(R.id.edit1); 
editText.setOnFocusChangeListener(new OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     EditText editText = (EditText)findViewById(R.id.edit1); 
     editText.setBackgroundColor(hasFocus ? Color.WHITE : Color.TRANSPARENT); 
     editText.setTextColor(hasFocus ? Color.BLACK : Color.GRAY); 
    } 
}); 
1

實現方法onFocusChabgeListener()如下所示。

textView.setOnFocusChangeListener(new OnFocusChangeListener(){ 

if(textView.hasFocuss){ 
//set textColor and background color 
} 
else 
{ 
//reset it 
} 

}); 
相關問題