首页 文章

AutocompleteTextView建议列表上升

提问于
浏览
18

可能重复Android : autocompletetextview, suggestion list displays above the textview?

当建议列表被用户滚动但我总是打开侧面时,我完全试图在键盘上显示重叠的建议列表 .

我来这里是这样的

enter image description here

这是我的清单文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sl"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SuggestionListActivity" 
            android:windowSoftInputMode="adjustResize|adjustPan|stateHidden">
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

这是我的main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:padding="10dp">

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" 
        android:layout_margin="10dp"/>

    <TextView android:layout_margin="10dp"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="This is testing for the auto complete textview in this application to display suggestion list overlapping on keyboard." />

    <AutoCompleteTextView android:id="@+id/autocomplete"
            android:layout_width="fill_parent" android:layout_margin="5dp"
            android:layout_height="wrap_content" android:hint="Search"
            android:layout_marginLeft="5dp" android:dropDownHeight="300dp"
            android:inputType="textAutoComplete" android:singleLine="true"
            />

</LinearLayout>

如何在列表聚焦时在此代码中执行以显示键盘上的建议 .

6 回答

  • 0

    我以前遇到过这个问题 . 对我来说, AutocompleteTextView 上面的屏幕空间比下面的更多(在"normal"设备上测试),因此列表向上打开 . 我稍微调整了我的布局,以便 AutocompleteTextView 下面有更多的空间,它开始向下打开 . 这就是为我解决的问题 .

  • 9

    您可以调整布局,以便 AutoCompleteTextView 下面有更多空间

    要么

    您可以更改下拉高度 android:dropDownHeight 并设置一些高值,这将在 scrollView 内部和 AutoCompleteTextView 靠近顶部时起作用 .

    要显示焦点上的选项列表,请执行以下操作

    autoCompleteTextView.setOnFocusChangeListener(new OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus) {
                autoCompleteTextView.showDropDown();
            }
        }
    });
    

    当用户关注 AutoCompleteTextView 时,这将显示一个选项列表

  • -2

    诀窍是确保所需的下拉高度永远不会大于下面的可用空间 . 我的方法是创建一个覆盖 showDropDown 的子类:

    public class DownOnlyAutoCompleteTextView extends AppCompatAutoCompleteTextView {
    
        private final static int MINIMAL_HEIGHT = 50;
    
        public DownOnlyAutoCompleteTextView(Context context) {
            super(context);
        }
    
        public DownOnlyAutoCompleteTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public DownOnlyAutoCompleteTextView(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        public void showDropDown() {
            Rect displayFrame = new Rect();
            getWindowVisibleDisplayFrame(displayFrame);
    
            int[] locationOnScreen = new int[2];
            getLocationOnScreen(locationOnScreen);
    
            int bottom = locationOnScreen[1] + getHeight();
            int availableHeightBelow = displayFrame.bottom - bottom;
            if (availableHeightBelow >= MINIMAL_HEIGHT) {
                setDropDownHeight(availableHeightBelow);
            }
    
            super.showDropDown();
        }
    }
    

    然后在你的布局中使用它,例如:

    <your.package.DownOnlyAutoCompleteTextView
        android:id="@+id/auto_complete_text_view"
        android:layout_margin="12dp"
        android:hint="AutoComplete"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:ignore="HardcodedText" />
    

    调整 MINIMAL_HEIGHT 以满足您的要求 - 如果有's no or very little space below, it'可能最好不要强制解决问题 .

  • 2

    只需将 android:dropDownHeight="100dp" 添加到布局文件中的 AutoCompleteTextView 标签就是我猜的最佳解决方案!它只会控制下降高度的高度,让我们滚动!

    <AutoCompleteTextView
            android:id="@+id/acetxt_assignclient"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"   
            android:dropDownHeight="250dp">
    </AutoCompleteTextView>
    
  • 0

    这是我的解决方案

    private final static int DELAY_MS = 500;
    autoCompletionTextView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                autoCompletionTextView.requestFocus();
                new Handler().postDelayed(() -> autoCompletionTextView.showDropDown(), DELAY_MS);
                return false;
            }
        });
    

    键盘显示建议列表后面列出了AutoCompletionTextView .

  • 6

    在Scrollview中设置包含Autocompletetextview的完整版面

    这将解决您的问题!

相关问题