不要在AutocompleteTextView上使用showDropDown

时间:2019-07-02 07:32:36

标签: android

我正在使用AutocompleteTextView。 showDropDown函数不起作用。如何激活此功能?如何运行showdropdown?我需要做吗?

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, OnItemSelectedListener, View.OnTouchListener, RadioGroup.OnCheckedChangeListener{
     private static final String[] paths = {"...."};
 @Override
    protected void onCreate(Bundle savedInstanceState) {

 final ArrayAdapter<String> arrayAdapterLocation = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item,paths);

final AutoCompleteTextView textViewLocationUpdate;        textViewLocationUpdate = (AutoCompleteTextView) findViewById(R.id.txtlocationUpdate);
  textViewLocationUpdate.setOnTouchListener(new OnTouchListener() {

            @SuppressLint("ClickableViewAccessibility")
            @Override
            public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
                // TODO Auto-generated method stub
                textViewLocationUpdate.showDropDown();
                textViewLocationUpdate.requestFocus();
                return false;
            }
        });
        textViewLocationUpdate.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

 final String selected = (String) parent.getItemAtPosition(position);
 final Object item = parent.getItemAtPosition(position);
        refDeviceName.child("title").addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) { dataSnapshot.getRef().setValue(selected); }
                    @Override
                    public void onCancelled(DatabaseError databaseError) { }});  }
        });

1 个答案:

答案 0 :(得分:0)

我了解您忘记将适配器添加到AutoCompleteTextView

final ArrayAdapter<String> arrayAdapterLocation = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item,paths);

final AutoCompleteTextView textViewLocationUpdate = (AutoCompleteTextView) findViewById(R.id.txtlocationUpdate);
textViewLocationUpdate.setAdapter(arrayAdapterLocation);

如果您想显示下拉菜单,甚至没有输入任何文本。在自定义AutoCompleteTextView

下使用
public class InstantAutoCompleteTextView extends AppCompatAutoCompleteTextView {

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

  public InstantAutoCompleteTextView(Context arg0, AttributeSet arg1) {
    super(arg0, arg1);
  }

  public InstantAutoCompleteTextView(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) {
      performFiltering(getText(), 0);
    }
  }
}

更改您的xml以使用此AutoCompleteTextView

 <com.example.ui.InstantAutoCompleteTextView
              android:id="@+id/txtlocationUpdate"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              android:padding="5dp"
              android:background="@android:color/transparent"
              android:completionThreshold="0"
              android:dropDownWidth="match_parent"
              android:gravity="center_vertical"
              android:minWidth="80dp"
              android:textColor="@color/primary_text"
              android:textColorHint="@color/disabled_text"
              android:textSize="@dimen/medium_font"/>
          </com.example.ui.InstantAutoCompleteTextView>

此外,添加此onClick侦听器:

txtlocationUpdate.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
         txtlocationUpdate.showDropDown();
      }
    });