2017-08-13 40 views
3

只要用戶在edittext中輸入「house」,就試着着色一個詞(房子)。這是我做了什麼:如果在edittext中包含該文字的話,如何爲android文本的一部分着色

if (textA.getText().toString().equals("house")){ 
    String name = String.valueOf(textA.getText().toString().equals("house")); 
    name.setTextColor(Color.parseColor("#bdbdbd")); 
} 

我的XML是如下

<LinearLayout 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="15dp" 
        android:layout_marginEnd="15dp" 
        android:orientation="vertical" 
        android:padding="5dp"> 


        <EditText 
         android:id="@+id/textA" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="5dp" 
         android:inputType="textMultiLine" 
         android:maxLines ="4" 
         android:maxLength ="2000" 
         android:textColorHint="@color/grey" 
         android:background="@android:color/transparent" 
         android:gravity="top"/> 

       </LinearLayout> 

然而,這會導致應用程序崩潰。不知道我做錯了什麼。我是Android新手。有什麼建議嗎?

+0

什麼是logcat的說?什麼是崩潰錯誤? – John61590

+0

你應該得到'name.setTextColor'的編譯時錯誤,因爲名稱是字符串(本地變量) – Manmohan

回答

1

這對我的作品

textA.addTextChangedListener(new TextWatcher() { 
     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

     } 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      String str="home"; 
      Spannable spannable =textA.getText(); 
      if(s.length() != 0) { 
       textA.setTextColor(Color.parseColor("#0000FF")); 
       ForegroundColorSpan fcs = new ForegroundColorSpan(Color.GREEN); 
       String s1 = s.toString(); 
       int in=0; // start searching from 0 index 

// keeps on searching unless there is no more function string found 
       while ((in = s1.indexOf(str,in)) >= 0) { 
        spannable.setSpan(
          fcs, 
          in, 
          in + str.length(), 
          Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
        // update the index for next search with length 
        in += str.length(); 
       } 
      } 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }); 
+0

的作品,但顏色變化適用於所有文本,我們如何使這適用於只有「家」whiles其他顏色保持正常嗎? – Simon

+0

意思是在edittext中只有「home」字樣應該包含不同的顏色? –

+0

究竟是什麼意思呢看起來好像不知道這個 – Simon

1

你必須寫代碼像下面

textA.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable s) {} @Override public void 
beforeTextChanged(CharSequence s, int start, int count, int after) { } 
@Override public void onTextChanged(CharSequence s, int start, int 
before, int count) { 
     if(s.length() != 0 && s.contains("home")) name.setTextColor(Color.parseColor("#bdbdbd")); 
    }); 
+0

我得到一個錯誤實現此代碼:( – Simon

+0

什麼是錯誤? –

+0

它可能是語法錯誤,因爲我從移動發佈它。 –

0

等於返回值是布爾值。 所以你必須改變它。

public boolean equals(Object anObject) 

    if (textA.getText().toString().equals("house")){ 
     textA.setTextColor(Color.parseColor("#bdbdbd")); 
    } 
相關問題