سؤال

انا استخدم AutoCompleteTextView, ، عندما ينقر المستخدم عليه ، أريد عرض الاقتراحات حتى لو لم يكن لديه نص - ولكن setThreshold(0) يعمل تمامًا مثل setThreshold(1) - لذلك يتعين على المستخدم إدخال حرف واحد على الأقل لإظهار الاقتراحات.

هل كانت مفيدة؟

المحلول

هذا هو سلوك موثق:

متى threshold أقل من أو يساوي 0 ، يتم تطبيق عتبة 1.

يمكنك إظهار المنسدلة يدويًا عبر showDropDown(), ، لذلك ربما يمكنك الترتيب لإظهاره عندما تريد. أو ، الفئة الفرعية AutoCompleteTextView والتجاوز enoughToFilter(), ، عودة true كل الوقت.

نصائح أخرى

ها هو صفي instantautocomplete. إنه شيء بين AutoCompleteTextView و Spinner.

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 && getAdapter() != null) {
            performFiltering(getText(), 0);
        }
    }

}

استخدمه في XML مثل هذا:

<your.namespace.InstantAutoComplete ... />

أسهل طريقة:

فقط استخدم setontouchListener و ShowDropDown ()

AutoCompleteTextView text;
.....
.....
text.setOnTouchListener(new View.OnTouchListener(){
   @Override
   public boolean onTouch(View v, MotionEvent event){
      text.showDropDown();
      return false;
   }
});

يعمل رمز القدر بشكل رائع عندما يكون هناك واحد فقط InstantAutoComplete هدف. لم يعمل مع اثنين رغم ذلك - لا فكرة لماذا. لكن عندما أضع showDropDown() (تمامًا مثل المشورة المشورة) onFocusChanged() مثله:

@Override
protected void onFocusChanged(boolean focused, int direction,
        Rect previouslyFocusedRect) {
    super.onFocusChanged(focused, direction, previouslyFocusedRect);
    if (focused) {
        performFiltering(getText(), 0);
        showDropDown();
    }
}

حل المشكلة.

إنها مجرد إجابتين مجتمعين بشكل صحيح ، لكنني آمل أن ينقذ شخص ما بعض الوقت.

المحول لا يؤدي التصفية في البداية.
عند عدم تنفيذ التصفية ، تكون قائمة القائمة المنسدلة فارغة.
لذلك قد تضطر إلى الحصول على التصفية في البداية.

للقيام بذلك ، يمكنك الاحتجاج filter() بعد الانتهاء من إضافة الإدخالات:

adapter.add("a1");
adapter.add("a2");
adapter.add("a3");
adapter.getFilter().filter(null);

إجابة Destil أعلاه تعمل تقريبًا ، ولكن لديها خطأ واحد خفي. عندما يركز المستخدم لأول مرة على الحقل ، فإنه يعمل ، ولكن إذا غادروا ثم يعود إلى الحقل ، فلن يُظهر المنسدلة لأن قيمة mpopupcanbeupdated ستظل خاطئة من عندما تم إخفاؤها. الإصلاح هو تغيير طريقة onfocuschanged إلى:

@Override
protected void onFocusChanged(boolean focused, int direction,
        Rect previouslyFocusedRect) {
    super.onFocusChanged(focused, direction, previouslyFocusedRect);
    if (focused) {
        if (getText().toString().length() == 0) {
            // We want to trigger the drop down, replace the text.
            setText("");
        }
    }
}

يمكنك استخدام onfocuschangelistener.

TCKimlikNo.setOnFocusChangeListener(new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                TCKimlikNo.showDropDown();

            }

        }
    });

لجعل customautocompleteTextView. 1. تجاوز العتبة ، طريقة كافية

public class CustomAutoCompleteTextView  extends AutoCompleteTextView { 

    private int myThreshold; 

    public CustomAutoCompleteTextView  (Context context) { 
        super(context); 
    } 

    public CustomAutoCompleteTextView  (Context context, AttributeSet attrs, int defStyle) { 
        super(context, attrs, defStyle); 
    } 

    public CustomAutoCompleteTextView  (Context context, AttributeSet attrs) { 
        super(context, attrs); 
    } 
     //set threshold 0.
    public void setThreshold(int threshold) { 
        if (threshold < 0) { 
            threshold = 0; 
        } 
        myThreshold = threshold; 
    } 
    //if threshold   is 0 than return true
    public boolean enoughToFilter() { 
         return true;
        } 
    //invoke on focus 
    protected void onFocusChanged(boolean focused, int direction,
            Rect previouslyFocusedRect) {
                    //skip space and backspace 
        super.performFiltering("", 67);
        // TODO Auto-generated method stub
        super.onFocusChanged(focused, direction, previouslyFocusedRect);

    }

    protected void performFiltering(CharSequence text, int keyCode) {
        // TODO Auto-generated method stub
        super.performFiltering(text, keyCode);
    }

    public int getThreshold() { 
        return myThreshold; 
    } 
}

ما عليك سوى استدعاء هذه الطريقة على اللمس أو انقر فوق حدث لـ AutoCompleTeTextView أو المكان الذي تريده.

autoCompleteTextView.showDropDown()

جربها

    searchAutoComplete.setThreshold(0);
    searchAutoComplete.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {//cut last probel
                    if (charSequence.length() > 1) {
                        if (charSequence.charAt(charSequence.length() - 1) == ' ') {
                            searchAutoComplete.setText(charSequence.subSequence(0, charSequence.length() - 1));
                            searchAutoComplete.setSelection(charSequence.length() - 1);
                        }
                    }
                   }


                @Override
                public void afterTextChanged(Editable editable) {
                }
            });


    //when clicked in autocomplete text view
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
              case R.id.header_search_etv:
                    if (searchAutoComplete.getText().toString().length() == 0) {
                        searchAutoComplete.setText(" ");
                    }
             break;
            }
        }):

هذا عمل بالنسبة لي ، رمز الزائفة:

    public class CustomAutoCompleteTextView extends AutoCompleteTextView {
    public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean enoughToFilter() {
        return true;
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (focused) {
            performFiltering(getText(), 0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.showDropDown();
        return super.onTouchEvent(event);
    }
}

ما عليك سوى الصق هذا في طريقة oncreate الخاصة بك في جافا

final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(
            this, android.R.layout.simple_spinner_dropdown_item,
            getResources().getStringArray(R.array.Loc_names));

    textView1 =(AutoCompleteTextView) findViewById(R.id.acT1);
    textView1.setAdapter(arrayAdapter);

    textView1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(final View arg0) {
            textView1.setMaxLines(5);
            textView1.showDropDown();

        }
    });

وهذا لملف XML الخاص بك ...

<AutoCompleteTextView
            android:layout_width="200dp"
            android:layout_height="30dp"
            android:hint="@string/select_location"
            android:id="@+id/acT1"
            android:textAlignment="center"/>

وإنشاء صفيف في string.xml تحت القيم ...

<string-array name="Loc_names">

        <item>Pakistan</item>
        <item>Germany</item>
        <item>Russia/NCR</item>
        <item>China</item>
        <item>India</item>
        <item>Sweden</item>
        <item>Australia</item>
    </string-array>

وأنت على ما يرام.

بعد سبع سنوات ، الرجال ، تظل المشكلة كما هي. إليك فئة ذات وظيفة تجبر تلك المنبثقة الغبية على إظهار نفسها في أي ظروف. كل ما عليك فعله هو ضبط محول على expleteTextView الخاص بك ، وإضافة بعض البيانات فيه ، والاتصال showDropdownNow() وظيفة في أي وقت.

ائتمانات إلى david vávra. إنه يعتمد على رمزه.

import android.content.Context
import android.util.AttributeSet
import android.widget.AutoCompleteTextView

class InstantAutoCompleteTextView : AutoCompleteTextView {

    constructor(context: Context) : super(context)

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    override fun enoughToFilter(): Boolean {
        return true
    }

    fun showDropdownNow() {
        if (adapter != null) {
            // Remember a current text
            val savedText = text

            // Set empty text and perform filtering. As the result we restore all items inside of
            // a filter's internal item collection.
            setText(null, true)

            // Set back the saved text and DO NOT perform filtering. As the result of these steps
            // we have a text shown in UI, and what is more important we have items not filtered
            setText(savedText, false)

            // Move cursor to the end of a text
            setSelection(text.length)

            // Now we can show a dropdown with full list of options not filtered by displayed text
            performFiltering(null, 0)
        }
    }
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top