带有下拉列表的android edittext

标签 android android-edittext

我有一个从用户那里获取值的编辑文本。我想添加一个选项,允许用户在单击编辑文本时通过下拉列表从不同的选项中进行选择。有谁知道如何做到这一点?

这是编辑文本代码:

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:id="@+id/dish_quantity"
    android:layout_below="@+id/dish_name"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:hint="Quantity" />

我希望它看起来像这样:

enter image description here

最佳答案

我认为这是你的要求:

只要按照这个例子: XML:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >   
<AutoCompleteTextView
    android:id="@+id/languages"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"></AutoCompleteTextView>
</LinearLayout>

Activity :-

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {

    String[] languages = { "C","C++","Java","C#","PHP","JavaScript","jQuery","AJAX","JSON" };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
                //Create Array Adapter
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice, languages);
                //Find TextView control
        AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.languages);
        //Set the number of characters the user must type before the drop down list is shown
                acTextView.setThreshold(1);
                //Set the adapter
        acTextView.setAdapter(adapter);
    }
}

演示:-

enter image description here

关于带有下拉列表的android edittext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31052436/

相关文章:

android - 普通字体在某些设备上显示为斜体。为什么以及如何解决?

java - 增加 TextView 的值,例如0为点击值+1

Android:在所有 EditTexts 禁用软键盘

Android:EditText 上的软键盘退格键不起作用

java - 安卓。如何捕捉键盘隐藏的时刻?

android - Xamarin.Forms TitleView FillAndExpand 不适用于 Android

android - id-beginner wonder 的加号和非加号

java - Toast动画中的CharSequence没有出现?安卓

Android 仅对 Edittext 禁用空间

android - 键盘打开时相应地向上滚动 RecyclerView