过去两天,我在这个问题上花费了最多的时间。实际上,我想要一个微调器,它的行为类似于 TextInputLayout 中的 EditText(花哨的提示,如果在上一个编辑文本中按下了下一个/输入键盘按钮,它就会流走并被选择/输入)。
这似乎是不可能的,所以我想出了这个:
<android.support.design.widget.TextInputLayout
android:id="@+id/newMeasure_layout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="@+id/title_layout"
app:layout_constraintStart_toStartOf="@+id/title_layout"
app:layout_constraintTop_toBottomOf="@+id/measureSpinner">
<AutoCompleteTextView
android:id="@+id/newMeasure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionNext|flagNoExtractUi"
android:singleLine="true"
android:inputType="textNoSuggestions|textVisiblePassword"
android:cursorVisible="false"
android:hint="@string/measure"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:drawableTint="@color/secondaryColor"
android:drawableEnd="@drawable/ic_arrow_drop_down_black_24dp"
android:drawableRight="@drawable/ic_arrow_drop_down_black_24dp" />
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
这可以防止键盘显示闪烁的光标,提供建议、更正、...
为了防止用户输入内容,但仍允许通过按 enter / next 来聚焦下一个输入,我在代码中设置了一个过滤器(它还检查文本在建议光标中是否可用)。
private AutoCompleteTextView mNewMeasure;
...
mNewMeasure = root.findViewById(R.id.newMeasure);
mNewMeasure.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
((AutoCompleteTextView)view).showDropDown();
return false;
}
});
mNewMeasure.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean b) {
AutoCompleteTextView v = ((AutoCompleteTextView)view);
if(b && v.getText().length() == 0) {
v.showDropDown();
} …Run Code Online (Sandbox Code Playgroud)