????? 此功能类似于临摹。已知被临摹的字符串,将字符串与EditText中输入进行
对比,输入错误,则用“×”替换所输入的字符。
import android.app.Activity;import android.os.Bundle;import android.text.InputFilter;import android.text.Spanned;import android.util.Log;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MyFilterTest extends Activity { /** Called when the activity is first created. */ TextView myText; EditText myEdit; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final String str = "Hello,Android!"; myText=(TextView)findViewById(R.id.myText); myText.setText(str); myEdit=(EditText)findViewById(R.id.myEdit); myEdit.setFilters(new InputFilter[]{ new MyInputFilter(str) }); } public class MyInputFilter implements InputFilter{ String str=null; public MyInputFilter(String str){ this.str=str; } @Override public CharSequence filter(CharSequence src, int start, int end, Spanned dest, int dstart, int dend) { // TODO Auto-generated method stub String ch=null; String TAG="Filter"; Log.w(TAG,"src:"+src+";start:"+start+";end:"+end); Log.w(TAG,"dest:"+dest+";dstart:"+dstart+";dend:"+dend); if(dest.length()<str.length()){ ch=str.substring(dstart+start, dstart+end); }else{ return dest.subSequence(dstart, dend); } if(ch.equals(src)){ Toast.makeText(MyFilterTest.this, "match", Toast.LENGTH_SHORT).show(); return dest.subSequence(dstart, dend)+src.toString(); }else{ Toast.makeText(MyFilterTest.this, "mismatch", Toast.LENGTH_SHORT).show(); return dest.subSequence(dstart, dend)+"×"; } } }}
?