and*_*ter 45 android autocompletetextview
我有一个AutocompleteTextView,它工作正常.当我写一个单词时,它会显示相关的结果,但我想显示所有项目而不在AutocompleteTextView中写任何单词.我怎样才能做到这一点.
Tal*_*lha 76
您需要扩展AutoCompleteTextView,
"当阈值小于或等于0时,应用阈值1.".
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在xml中
<AutoCompleteTextView ... /> to <your.namespace.InstantAutoComplete ... />
Run Code Online (Sandbox Code Playgroud)
编辑1
创建名为InstantAutoComplete的新类,然后将此代码放入类中.
在你的布局中,xml使用这个类
然后在你的actity中找到这个小部件(onCreate方法).
Mel*_*pes 40
这里更好的解决方案
您无需自定义AutoCompleteTextView.相反,autoCompleteTextView.showDropDown()只要你需要它就打电话.....欢呼:)
小智 26
这个对我有用:
添加到您的对象下一个事件方法:
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;
}
});
Run Code Online (Sandbox Code Playgroud)
Ho *_*ong 12
这对我来说非常有用,这是解决问题的简单方法:
final ArrayAdapter<String> 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;
}
});
Run Code Online (Sandbox Code Playgroud)
您需要调用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;
}
});
Run Code Online (Sandbox Code Playgroud)
你需要把这些步骤让它完美地工作
1-在你的 xml 中放这个
<androidx.appcompat.widget.AppCompatAutoCompleteTextView
android:id="@+id/account_type_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/_16sdp"
android:layout_marginTop="@dimen/_24sdp"
android:layout_marginEnd="@dimen/_16sdp"
android:background="@drawable/rounded_edt_with_border"
android:completionThreshold="0"
android:drawableRight="@drawable/ic_arrow_down"
android:hint="@string/account_type"
android:imeOptions="actionNext"
android:inputType="text"
android:padding="12dp"
android:textSize="@dimen/_15sp"
/>
Run Code Online (Sandbox Code Playgroud)
你只需要设置android:completionThreshold为零
2-在你的java代码中放入
mViewDataBinding.accountTypeSpinner.setOnFocusChangeListener((v, hasFocus) -> {
if (hasFocus)
mViewDataBinding.accountTypeSpinner.showDropDown();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
33973 次 |
| 最近记录: |