![](/img/trans.png)
[英]Remove overscroll effect on AutoCompleteTextView popup (Android Studio / Java / XML)
[英]Android AutoCompleteTextView popup moving after displaying
當我使用autocompletetextview時,一切正常,除了它在兩個位置之間切換:正確的一個位於textview下面並且相當低一些。 它開始出錯,但幾乎立即移動到正確的位置。 然而,當輸入或退格時,這對於每個字母都是非常煩人的。 我正在使用android studio。
似乎兩個事件同時試圖決定布局。 有時它會粘在一個位置或另一個位置。
**我使用自定義適配器減慢了過濾過程,看起來當輸入文本時它會移動到錯誤的位置,然后當過濾完成后,它會移回正確的位置。
不正確
正確:
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,我會在正確的值和此之間閃爍 -
要更改此位置,請使用dropDownAnchor屬性並引用另一個視圖ID。 (在自動填充下查看)
機器人:dropDownAnchor
查看以錨定自動完成下拉菜單。 如果未指定,則使用文本視圖本身。
你有很多下拉屬性..
最好使用實現Filter來自動完成適配器並從自動完成的OnQueryTextListener傳遞輸入的文本,並通過調用adapter.getitem設置所選文本。
將您的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" />
您是否有機會在其他AutoCompleteTextView上使用相同的ID? 我可以通過包含兩個具有相同id的視圖來重現您描述的行為。
出於某種原因,我在使用最新API時遇到了同樣的問題。 我通過在XML和onTextChanged事件中顯式設置dropDownAnchor , dropDownVerticalOffset和dropDownHeight來解決問題。
編輯:經過一些檢查后,似乎這種行為是以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並以dp為單位以編程方式設置寬度和高度
您必須設置dropDownHeight的原因是因為如果列表超出視圖的高度邊界,Android會有將其推送到屏幕頂部的行為,這進一步使移動問題復雜化。 如果你真的不介意(有些人不這樣做,取決於你的視圖設置),你可以從程序和XML中刪除dropDownHeight。
我應該注意到,這些變化仍然沒有使Marshmallow和其他API之間的變化完全相同。 看起來仍有一些像素差異。 但它非常接近。
我遇到了同樣的問題,太多小時后我找到了解決方案。
你應該改變:
android:layout_width="wrap_content"
到android:layout_width="match_parent"
我希望它對你也有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.