扩展EDITTEXT类,并添加自定义属性,以检测正确的答案

问题描述:

我想设计一个扩展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。这是没有定义在您的项目 –

您可以使用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) { 

     } 

    };