Я хочу создать EditText
с интерактивным меню под ним. Пользователь должен иметь возможность писать свой собственный текст в EditText
или выбирать из предопределенных опций.
Я, однако, о добавлении recyclerView
в EditText
, но для 4-5 вариантов его отходов recylerView
. Есть ли способ сделать это лучше (я не хочу писать много кода для этой функции).
Всего 2 ответа
AutoCompleteTextView - это компонент, используемый для отображения предложений при записи в редактируемом текстовом поле. Список предложений отображается в раскрывающемся меню, из которого пользователь может выбрать нужный элемент. Список предложений получен от адаптера, и он появляется только после нескольких символов, указанных в пороговом значении.
Вот простой пример AutoCompleteTextView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:text="X" />
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView"
android:layout_marginLeft="36dp"
android:layout_marginTop="17dp"
android:ems="10"
android:text="">
<requestFocus />
</AutoCompleteTextView>
</RelativeLayout>
MainActivity.java определен ниже.
public class MainActivity extends Activity {
String[] animals = {"Ant", "Bbird", "Cat"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating the instance of ArrayAdapter containing list of fruit names
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.select_dialog_item, animals);
//Getting the instance of AutoCompleteTextView
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
actv.setThreshold(1);//will start working from first character
actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView
actv.setTextColor(Color.BLACK);
}
}
попробуйте это, xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="State:" />
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Джава
public class MainActivity extends AppCompatActivity {
AutoCompleteTextView autoCompleteTextView;
String[] arr={"used","new","repaired"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
autoCompleteTextView=findViewById(R.id.autoCompleteTextView);
ArrayAdapter aa=new ArrayAdapter(this,android.R.layout.simple_list_item_1,arr);
autoCompleteTextView.setAdapter(aa);
}
}