Show all items in AutocompleteTextView without writing text

前端 未结 12 754
萌比男神i
萌比男神i 2020-12-29 01:05

I have a AutocompleteTextView and it works fine. When I write a word it shows the relevant result but I want to show all items without writing any word in AutocompleteTextVi

相关标签:
12条回答
  • 2020-12-29 01:36

    Here an approach with onclicklistener as I found tat onTouch was a bit irritating when trying to scroll. mOccupation is the AutocompleteTextView in question.

        mOccupation=(AutoCompleteTextView) findViewById(R.id.actv_occupation);
        ArrayAdapter<String> occupationAdapter=new ArrayAdapter<String> 
        (NewClientActivity.this,
                android.R.layout.simple_list_item_1,
                getResources().getStringArray(R.array.occupation_array));
        mOccupation.setAdapter(occupationAdapter);
        mOccupation.setKeyListener(null);
        mOccupation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //mOccupation.setText(null);
                ((AutoCompleteTextView) view).showDropDown();
                return;
            }
        });
    

    I managed to put it all into a Textinputlayout with the following xml specifications:

    <android.support.design.widget.TextInputLayout
        android:id="@+id/lo_occupation"
        android:layout_marginTop="10dp"
        android:layout_gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <AutoCompleteTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="occupation"
            android:focusableInTouchMode="false"<--this is the important part
            android:id="@+id/actv_occupation"
            android:ems="10"
            android:completionThreshold="0"<--this too
            />
    </android.support.design.widget.TextInputLayout>
    
    0 讨论(0)
  • 2020-12-29 01:39

    You could simply use this single line of code

    autoCompleteTextView.setThreshold(100);
    
    0 讨论(0)
  • 2020-12-29 01:40

    You need to call requestFocus(); to show keyboard, otherwise keyboard does not pop up.

    The method forcefully shows drop-down list.

    autocomptv.setOnTouchListener(new OnTouchListener() {
    
            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
                // TODO Auto-generated method stub
                autocomptv.showDropDown();
                autocomptv.requestFocus();
                return false;
            }
        });
    
    0 讨论(0)
  • 2020-12-29 01:42

    This works for me perfectly, this is an easy way to resolve the problem:

    final ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_dropdown_item_1line, usernameLists);
    etUsername.setThreshold(1);
    etUsername.setAdapter(adapter);
    etUsername.setOnTouchListener(new View.OnTouchListener() {
    
        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
            if (usernameLists.size() > 0) {
                    // show all suggestions
                    if (!etUsername.getText().toString().equals(""))
                        adapter.getFilter().filter(null);
                    etUsername.showDropDown();
                }
            return false;
        }
    });
    
    0 讨论(0)
  • 2020-12-29 01:42

    you need to put those steps to make it work perfectly

    1-in your xml put this

        <androidx.appcompat.widget.AppCompatAutoCompleteTextView
                android:id="@+id/account_type_spinner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/_16sdp"
                android:layout_marginTop="@dimen/_24sdp"
                android:layout_marginEnd="@dimen/_16sdp"
                android:background="@drawable/rounded_edt_with_border"
                android:completionThreshold="0"
                android:drawableRight="@drawable/ic_arrow_down"
                android:hint="@string/account_type"
                android:imeOptions="actionNext"
                android:inputType="text"
                android:padding="12dp"
                android:textSize="@dimen/_15sp"
                 />
    

    you just need to set android:completionThreshold to zero

    2- in your java code put

      mViewDataBinding.accountTypeSpinner.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus)
                mViewDataBinding.accountTypeSpinner.showDropDown();
        });
    
    0 讨论(0)
  • 2020-12-29 01:49

    use this :

     text.setOnTouchListener(new View.OnTouchListener(){
    
    
                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {
                    // TODO Auto-generated method stub
                    text.showDropDown();
                    return false;
                }
                });
    
    0 讨论(0)
提交回复
热议问题