android 自定义autocompletetextview,android-在AutocompleteTextView中显示所有项目,而无需编写tex...

android-在AutocompleteTextView中显示所有项目,而无需编写tex

我有一个AutocompleteTextView,它工作正常。 当我写一个单词时,它会显示相关结果,但我想显示所有项目,而无需在AutocompleteTextView中写任何单词。 我怎样才能做到这一点。

9个解决方案

75 votes

您需要扩展AutoCompleteTextView,

“当阈值小于或等于0时,阈值1为 应用。”。

setThreshold

import android.content.Context;

import android.graphics.Rect;

import android.util.AttributeSet;

import android.widget.AutoCompleteTextView;

public class InstantAutoComplete extends AutoCompleteTextView {

public InstantAutoComplete(Context context) {

super(context);

}

public InstantAutoComplete(Context arg0, AttributeSet arg1) {

super(arg0, arg1);

}

public InstantAutoComplete(Context arg0, AttributeSet arg1, int arg2) {

super(arg0, arg1, arg2);

}

@Override

public boolean enoughToFilter() {

return true;

}

@Override

protected void onFocusChanged(boolean focused, int direction,

Rect previouslyFocusedRect) {

super.onFocusChanged(focused, direction, previouslyFocusedRect);

if (focused && getFilter()!=null) {

performFiltering(getText(), 0);

}

}

}

在xml中

to

编辑1

创建名为InstantAutoComplete的新类,然后将此代码放入该类。

在您的布局xml中使用此类,例如

然后在您的活动中找到此小部件(onCreate方法)。

看这个例子

Talha answered 2020-07-14T00:19:26Z

40 votes

更好的解决方案

您不需要自定义您的AutoCompleteTextView,而只要需要时只需致电autoCompleteTextView.showDropDown() ..... cheers :)

Melbourne Lopes answered 2020-07-14T00:19:50Z

26 votes

这个对我有用:

向您的对象添加下一个事件方法:

myView.setOnFocusChangeListener(new OnFocusChangeListener() {

@Override

public void onFocusChange(View v, boolean hasFocus) {

if (hasFocus)

myView.showDropDown();

}

});

myView.setOnTouchListener(new OnTouchListener() {

@Override

public boolean onTouch(View v, MotionEvent event) {

myView.showDropDown();

return false;

}

});

Ubirajara Erthal answered 2020-07-14T00:20:14Z

12 votes

这非常适合我,这是解决问题的简便方法:

final ArrayAdapter adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_dropdown_item_1line, usernameLists);

etUsername.setThreshold(1);

etUsername.setAdapter(adapter);

etUsername.setOnTouchListener(new View.OnTouchListener() {

@SuppressLint("ClickableViewAccessibility")

@Override

public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {

if (usernameLists.size() > 0) {

// show all suggestions

if (!etUsername.getText().toString().equals(""))

adapter.getFilter().filter(null);

etUsername.showDropDown();

}

return false;

}

});

Ho Luong answered 2020-07-14T00:20:34Z

4 votes

您需要调用requestFocus(); 显示键盘,否则键盘不会弹出。

方法强制显示下拉列表。

autocomptv.setOnTouchListener(new OnTouchListener() {

@SuppressLint("ClickableViewAccessibility")

@Override

public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {

// TODO Auto-generated method stub

autocomptv.showDropDown();

autocomptv.requestFocus();

return false;

}

});

Hemant Shori answered 2020-07-14T00:20:58Z

4 votes

用这个 :

text.setOnTouchListener(new View.OnTouchListener(){

@Override

public boolean onTouch(View arg0, MotionEvent arg1) {

// TODO Auto-generated method stub

text.showDropDown();

return false;

}

});

answered 2020-07-14T00:21:18Z

0 votes

如果其他解决方案不适合您,请尝试执行此操作。 始终在单击时显示弹出窗口。

public class InstantAutoComplete extends AppCompatAutoCompleteTextView {

public InstantAutoComplete(Context context) {

super(context);

}

public InstantAutoComplete(Context arg0, AttributeSet arg1) {

super(arg0, arg1);

}

public InstantAutoComplete(Context arg0, AttributeSet arg1, int arg2) {

super(arg0, arg1, arg2);

}

@Override

public boolean enoughToFilter() {

return true;

}

@Override

public boolean onTouchEvent(MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN) {

performClick();

}

return super.onTouchEvent(event);

}

@Override

public boolean performClick() {

if (getFilter() != null && !isPopupShowing()) {

performFiltering(getText(), 0);

showDropDown();

}

return super.performClick();

}

}

Jan Moravec answered 2020-07-14T00:21:38Z

0 votes

在这里,我发现tat onTouch的onclicklistener方法在尝试滚动时有点恼人。 mOccupation是有问题的AutocompleteTextView。

mOccupation=(AutoCompleteTextView) findViewById(R.id.actv_occupation);

ArrayAdapter occupationAdapter=new ArrayAdapter

(NewClientActivity.this,

android.R.layout.simple_list_item_1,

getResources().getStringArray(R.array.occupation_array));

mOccupation.setAdapter(occupationAdapter);

mOccupation.setKeyListener(null);

mOccupation.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

//mOccupation.setText(null);

((AutoCompleteTextView) view).showDropDown();

return;

}

});

我设法将其全部放入具有以下xml规范的Textinputlayout中:

android:id="@+id/lo_occupation"

android:layout_marginTop="10dp"

android:layout_gravity="center_horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="occupation"

android:focusableInTouchMode="false"

android:id="@+id/actv_occupation"

android:ems="10"

android:completionThreshold="0"

/>

quealegriamasalegre answered 2020-07-14T00:22:03Z

0 votes

Nothing Custom Required.

我尝试了所有解决方案,但在某些情况下不起作用。 例如 一个解决方案是第一次使用,但是当您删除文本时,它不会 出现。 所以我挖了更多,找到了下面的解决方案。

欢迎提出建议。

XML:

android:id="@+id/tl"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:id="@+id/autoComplete"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:hint="Hint Here" />

科特林:

val adapter = ArrayAdapter(context, android.R.layout.select_dialog_item, listItems)

autoComplete.setAdapter(adapter)

//threshold specifies the minimum number of characters the user has to type in

//the

//edit box before the drop down list is shown

autoComplete.threshold = 0

//we have to add check for 0 number of character in edit text. When that

//happens, we will show pop up manually

autoComplete.addTextChangedListener(object : TextWatcher {

override fun afterTextChanged(s: Editable?) {}

override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}

override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

//first check if length of input is 0

if(s?.length ?: 0 == 0){

//if you don't use handler with post delay, the API will hide pop

//up, even if you show it. There could be better ways to this, but

//I have implemented this and after 100 millis it gives an animated

//look

Handler().postDelayed({

//manually show drop down

autoComplete.showDropDown()

}, 100) // with 100 millis of delay

}

}

})

//when user focus out the view, drop down vanishes. When come back it will not

//show, so to cover this scenario add following.

autoComplete.setOnFocusChangeListener { _, hasFocus ->

//when gain focus manually show drop down

if(hasFocus)

autoComplete.showDropDown()

}

Wajid Ali answered 2020-07-14T00:22:36Z

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值