【问题标题】:Android AutoCompleteTextView popup moving after displayingAndroid AutoCompleteTextView 弹出窗口在显示后移动
【发布时间】:2016-05-24 22:44:16
【问题描述】:

当我使用 autocompletetextview 时,一切正常,只是它不断在两个位置之间切换:正确的位置在 textview 的正下方,而位置则低得多。它开始错误,但几乎立即移动到正确的位置。然而,这在键入或退格时非常烦人,因为每个字母都会发生这种情况。我正在使用安卓工作室。

看起来好像两个事件同时试图决定布局。有时它会粘在一个位置上。

**我使用自定义适配器减慢了过滤过程,看起来当输入文本时它会移动到不正确的位置,然后在过滤完成后它会移动回正确的位置。

不正确

正确:

java(在 OnCreate() 中)-

String[] drugs = new String[]{"Nexium","Amoxicillin","LEVOCETIRIZINE DIHYDROCHLORIDE", "Advil", "Advair Diskus", "Daraprim"};

    AutoCompleteTextView drugNameAutoComplete = ((AutoCompleteTextView) findViewById(R.id.drugNameEditText));
    drugNameAutoComplete.setAnimation(null);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,drugs);
    drugNameAutoComplete.setAdapter(adapter);

还有布局代码-

<AutoCompleteTextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/drugNameEditText"
            android:enabled="true"
            android:singleLine="true"
            android:layout_below="@+id/lookingForView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:dropDownVerticalOffset="50dp"
            android:hint="@string/drug_name" />

如果我删除 dropDownVeticalOffset,我会在正确值和 this 之间闪烁-

【问题讨论】:

  • 尝试指定高度?

标签: java android xml android-studio


【解决方案1】:

要更改此位置,请使用 dropDownAnchor 属性并引用另一个视图 ID。 (在自动完成下查看)

android:dropDownAnchor

将自动完成下拉菜单锚定到的视图。如果未指定,则使用文本视图本身。

你有很多下拉属性..

您最好使用实现过滤器来自动完成适配器,并从自动完成的 OnQueryTextListener 传递输入的文本,并通过调用 adapter.getitem 设置选定的文本。

【讨论】:

  • 我试过了,但什么也没有。我不确定过滤与布局有什么关系。
【解决方案2】:

将您的 xml 更改为 -

<AutoCompleteTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/drugNameEditText"
    android:enabled="true"
    android:singleLine="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    **android:dropDownVerticalOffset="0dp"**
    **android:dropDownAnchor="@id/drugNameEditText"**
    android:hint="drug" />

【讨论】:

  • 这没有帮助。这些也是默认值,所以我看不出它们会产生什么影响?
【解决方案3】:

您是否偶然在另一个 AutoCompleteTextView 上使用了相同的 ID?我可以通过包含两个具有相同 ID 的视图来重现您描述的行为。

【讨论】:

  • 不 :( 只是检查然后重命名它只是为了确定。
【解决方案4】:

由于某种原因,我在使用最新的 API 时遇到了同样的问题。我通过在 XML 和 onTextChanged 事件中显式设置 dropDownAnchordropDownVerticalOffsetdropDownHeight 解决了这个问题。

编辑:经过一番检查,这种行为似乎与以 Marshmallow (API 23) 开头的 API 不一致。尝试将您的代码更改为以下内容:

final float scale = context.getResources().getDisplayMetrics().density;
int verticalOffsetDP;
int dropDownHeightDP = (int) (200 * scale + 0.5f);

int currentApiVersion = android.os.Build.VERSION.SDK_INT;

if (currentApiVersion < Build.VERSION_CODES.M)
{
    // Prior to Marshmallow DropDownVerticalOffset works as expected
    verticalOffset = 0;
}
else
{
    // Marshmallow has some weird DropDownVerticalOffset behavior so we have to compensate
    verticalOffset = (int) (50 * scale + 0.5f);
}

AutoCompleteTextView drugNameAutoComplete = ((AutoCompleteTextView)findViewById(R.id.drugNameEditText));
drugNameAutoComplete.setAnimation(null);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,drugs);
drugNameAutoComplete.setAdapter(adapter);

drugNameAutoComplete.setDropDownAnchor(R.id.drugNameEditText);
drugNameAutoComplete.setDropDownVerticalOffset(verticalOffsetDP);
drugNameAutoComplete.setDropDownHeight(dropDownHeightDP);

你的 XML 如下:

<AutoCompleteTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/drugNameEditText"
        android:enabled="true"
        android:singleLine="true"
        android:layout_below="@+id/lookingForView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:dropDownVerticalOffset="50dp"
        android:dropDownAnchor="@id/drugNameEditText"
        android:dropDownHeight="200dp"
        android:hint="@string/drug_name" />

请注意,您必须根据代码中的屏幕尺寸计算 DP。我从这个线程中优秀的接受响应中得到了代码示例:Android and setting width and height programmatically in dp units

您还必须设置 dropDownHeight 的原因是,如果列表超出视图的高度边界,Android 会有将其推到屏幕顶部的行为,从而使移动问题进一步复杂化。如果您实际上不介意这一点(有些人不介意,这取决于您的视图设置),您可以从程序化和 XML 中删除 dropDownHeight。

我应该注意到,这些更改仍然不能使 Marshmallow 和其他 API 之间的内容完全相同。看起来仍然存在一些像素差异。但它已经非常接近了。

【讨论】:

    【解决方案5】:

    我遇到了同样的问题,但过了好几个小时我才找到解决方案。

    你应该改变:

    android:layout_width="wrap_content"android:layout_width="match_parent"

    我希望它也对你有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-29
      相关资源
      最近更新 更多