註:下面例子是工程: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 stubSystem.out.println("--->before");}public void afterTextChanged(Editable s) {// TODO Auto-generated method stubSystem.out.println("--->after");}public void onTextChanged(CharSequence s, int start, int before,int count) {// TODO Auto-generated method stubSystem.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 exceptione.printStackTrace();}}}