我有一个从用户那里获取值的编辑文本。我想添加一个选项,允许用户在单击编辑文本时通过下拉列表从不同的选项中进行选择。有谁知道如何做到这一点?
这是编辑文本代码:
<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" />
我希望它看起来像这样:
最佳答案
我认为这是你的要求:
只要按照这个例子: 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);
}
}
演示:-
关于带有下拉列表的android edittext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31052436/