問題描述
當我寫 "#data" 時,我在 EditText
中寫了一些文本,它的顏色應該改變但不會改變我該怎么做.請檢查以下我使用過的EditText
I am writing some text in EditText
when i write "#data" its colour should be changed but doesn't change how can i do. Please check the below EditText
which i have used
<EditText
android:id="@+id/et_simple"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</EditText>
推薦答案
希望這個解決方案會有所幫助..!
Hope this solution will help..!
我使用了這個解決方案,非常有用!就像在你的 editText 上添加 textWatcher 接口并收聽 textChange 并找出單詞是否以 hashTag 開頭然后調用該單詞的 Change The color 方法!它有一些缺陷,但這些都是可以忽略不計的,請看這個簡單的.
I used this solution it is very useful! like adding the textWatcher interface over your editText and listening to textChange and finding out if the word starts with a hashTag then call the Change The color method on that word! it has some flaws but those are ignorable see this simple one here.
Spannable mspanable;
int hashTagIsComing = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText edtTxtMine = (EditText) findViewById(R.id.editText1);
mspanable = edtTxtMine.getText();
edtTxtMine.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String startChar = null;
try{
startChar = Character.toString(s.charAt(start));
Log.i(getClass().getSimpleName(), "CHARACTER OF NEW WORD: " + startChar);
}
catch(Exception ex){
startChar = "";
}
if (startChar.equals("#")) {
changeTheColor(s.toString().substring(start), start, start + count);
hashTagIsComing++;
}
if(startChar.equals(" ")){
hashTagIsComing = 0;
}
if(hashTagIsComing != 0) {
changeTheColor(s.toString().substring(start), start, start + count);
hashTagIsComing++;
}
}
@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
}
});
}
private void changeTheColor(String s, int start, int end) {
mspanable.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color)), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
這篇關于如何在 EditText 中以不同顏色標記值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!