注:下面例子是工程:mydictionary的一些片段,其中也涵盖了对文件的读写
1、声明
private AutoCompleteTextView mytext;
2、调用
mytext = (AutoCompleteTextView) findViewById(R.id.edit);
mytext.setThreshold(1);//设置输入一个字符就开始执行
TextChangeListener change = new TextChangeListener();
mytext.addTextChangedListener(change);
3、TextChangeListener监听器
public class TextChangeListener implements TextWatcher {
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
System.out.println("--->before");
}
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
System.out.println("--->after");
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
System.out.println("--->on");
InputWhere(s.toString());
try {
list = new ArrayList<String>();
list.clear();
RandomAccessFile in = new RandomAccessFile(FILENAME, "r");
for (int k = 0; k < 30; k++) {
in.seek(2048 + 54 * (mid + k - 3));
char chword[] = new char[25];
int i = 0;
byte buffer1 = in.readByte();
byte buffer2 = in.readByte();
while (!((buffer1 == 0) && (buffer2 == 0))) {
chword[i] = (char) ((buffer2 << 8) | (buffer1 & 0xff));
buffer1 = in.readByte();
buffer2 = in.readByte();
i++;
if ((2 + i * 2) >= 50)
break;
if (mid + k > 9119)
break;
}
StringBuffer sub = new StringBuffer();
sub.append(chword);
sub.setLength(i);
list.add(sub.toString());
}
mytext.setAdapter(new ArrayAdapter<String>(mydictionary.this,
android.R.layout.simple_dropdown_item_1line, list));
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}