溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android開發(fā)中怎么實現(xiàn)一個輸入框提示功能

發(fā)布時間:2020-11-26 15:46:35 來源:億速云 閱讀:146 作者:Leah 欄目:移動開發(fā)

這篇文章給大家介紹Android開發(fā)中怎么實現(xiàn)一個輸入框提示功能,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

可以使用cursor來動態(tài)加載AutoCompleteTextView的數(shù)據(jù),從而 實現(xiàn)時時搜索提示,要實現(xiàn)動態(tài)加載,只用重寫一個類繼承于CursorAdapter,然后設(shè)定在AutoCompleteTextView上就行了。

Android開發(fā)中怎么實現(xiàn)一個輸入框提示功能

AutoCompleteTextView editNumber = (AutoCompleteTextView)findViewById(R.id.edit_number);
Cursor cursor = getContentResolver()(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
ContactListAdapter listAdapter = new ContactListAdapter(this, cursor);
editNumber.setAdapter(listAdapter);

ContactListAdapter.java中的核心代碼如下:

重寫newView方法

public View newView(Context context, Cursor cursor, ViewGroup parent) {
  final LayoutInflater inflater = LayoutInflater.from(context);
  final View view = (View)inflater.inflate(  R.layout.auto_complete, parent, false);
  TextView txtName = (TextView)view.findViewById(R.id.txt_name);
  txtName.setText(cursor.getString(0));
  TextView txtNumber = (TextView)view.findViewById(R.id.txt_number);
  txtNumber.setText(cursor.getString(1));
  TextView txtType = (TextView)view.findViewById(R.id.txt_type);
  String[] arrType = SmsConstant.ARR_CONTACTS_TYPE;
  if(cursor.getint(2) > 3)
  {
    txtType.setText(arrType[0]);
  } else
  {
    txtType.setText(arrType[cursor.getint(2)]);
  }
  return view;
}

重寫bindView方法,

public void bindView(View view, Context context, Cursor cursor) {
  TextView txtName = (TextView)view.findViewById(R.id.txt_name);
  txtName.setText(cursor.getString(0));
  TextView txtNumber = (TextView)view.findViewById(R.id.txt_number);
  txtNumber.setText(cursor.getString(1));
  TextView txtType = (TextView)view.findViewById(R.id.txt_type);
  String[] arrType = SmsConstant.ARR_CONTACTS_TYPE;
  if(cursor.getint(2) > 3)
  {
    txtType.setText(arrType[0]);
  } else {
    txtType.setText(arrType[cursor.getint(2)]);
  }
}

點擊彈出的Listview列表后的返回值:

public String convertToString(Cursor cursor) {}

執(zhí)行搜索的sql語句,返回一個Cursor加載到彈出的Listview上

public Cursor runQueryOnBackgroundThread(CharSequence constraint) {}

在此所返回的Cursor結(jié)果,會全部顯示在彈出提示上,無需再次過慮。

關(guān)于Android開發(fā)中怎么實現(xiàn)一個輸入框提示功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI