Android学习笔记(3)---AutoCompleteTextView的使用

注:下面例子是工程: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();
			}
		}

	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值