2017-09-16 21 views
0

我想設計一個擴展EDITTEXT一類這樣擴展EDITTEXT類,並添加自定義屬性,以檢測正確的答案

1)有新的自定義屬性命名爲「realanswer」包含字符串值

2)這個類改變焦點後,通過這個類中的一個方法自動地(在主活動中帶有out定義的監聽器)檢測輸入值是否等於「realanswer」!如果答案爲真,則使textcolor變爲綠色,否則讀取。 這是我的新類:

package com.faridi.myapplication; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.util.AttributeSet; 
import android.widget.EditText; 

public class MFEditText extends EditText { 
public Context mcontext; 
public String realanswer; 
public MFEditText(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    this.mcontext=context; 
    TypedArray typedArray = 
    context.obtainStyledAttributes(attrs,R.styleable.answer); 
    String attr = 
    typedArray.getString(R.styleable.answer_answerAttribute); 
    setCustomAttribute(attr); 
    this.realanswer=attr; 
    Toast.makeText(MFEditText.this.mcontext, 
      attr,Toast.LENGTH_LONG).show(); 
     typedArray.recycle(); 
     this.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) {   
     if(MFEditText.this.getText().toString(). 
     equals(MFEditText.this.realanswer)){ 
       MFEditText.this.setTextColor(Color.GREEN); 
      }else { 
       MFEditText.this.setTextColor(Color.RED); 
      } 
      Toast.makeText(MFEditText.this.mcontext, 
        MFEditText.this.realanswer+ 
     " "+MFEditText.this.getText(),Toast.LENGTH_LONG).show(); 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 
    }); 

} 
private void setCustomAttribute(String attr) { 

    realanswer=attr; 
} 
private String getCustomAttribute() { 

    return realanswer; 
} 
} 

,這是我attr.xml文件

<?xml version="1.0" encoding="utf-8"?> 
<resources> 

<declare-styleable name="answer"> 

    <attr name="answerAttribute" format="string"/> 

</declare-styleable> 


</resources> 

,這是我如何使用它在我的mainlayout

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:custom="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" android:layout_width="match_parent" 
android:layout_margin="25dp" 
android:layout_height="match_parent"> 

<com.faridi.myapplication.MFEditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    custom:answerAttribute="33" 
    android:id="@+id/mfet" 
    /> 
<EditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" /> 
</LinearLayout> 

現在的工作。問題已經解決了。

+0

你爲什麼不使用textwatcher –

+0

我用,但我不知道爲什麼attr爲空? – masoud

+0

其中是R.styleable.answer_answerAttribute,這是null。這是沒有定義在您的項目 –

回答

0

您可以使用TextWatcherEditText檢查答案是否realanswer還是不喜歡這個

TextWatcher addremark_text_watcher = 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) { 

      // write your logic here 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 

     } 

    };