android edittext with drop down list

£可爱£侵袭症+ 提交于 2019-12-03 09:47:54

问题


I have an edittext which gets values from the user. I want to add an option which allows the user to choose from different options via drop down list when edittext is clicked. Does anyone have an idea how to do this?

This is edittext code:

<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" />

I want it to look like this:


回答1:


I think this is your requirement:

just follow this example: 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);
    }
}

Demo:-




回答2:


activity_main.xml

<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context=".MainActivity" >  

    <TextView  
        android:id="@+id/textView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_alignParentTop="true"  
        android:layout_marginTop="15dp"  
        android:text="@string/what_is_your_favourite_programming_language_" />  

    <AutoCompleteTextView  
        android:id="@+id/autoCompleteTextView1"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_below="@+id/textView1"  
        android:layout_marginLeft="36dp"  
        android:layout_marginTop="17dp"  
        android:ems="10"  
        android:text="">  

        <requestFocus />  
    </AutoCompleteTextView>  

</RelativeLayout>

MainActivity.java

import android.os.Bundle;  
import android.app.Activity;  
import android.graphics.Color;  
import android.view.Menu;  
import android.widget.ArrayAdapter;  
import android.widget.AutoCompleteTextView;  

public class MainActivity extends Activity {  
    String[] language ={"C","C++","Java",".NET","iPhone","Android","ASP.NET","PHP"};  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  

        //Creating the instance of ArrayAdapter containing list of language names  
           ArrayAdapter<String> adapter = new ArrayAdapter<String>  
            (this,android.R.layout.select_dialog_item,language);  
        //Getting the instance of AutoCompleteTextView  
           AutoCompleteTextView actv= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);  
           actv.setThreshold(1);//will start working from first character  
           actv.setAdapter(adapter);//setting the adapter data into the AutoCompleteTextView  
           actv.setTextColor(Color.RED);  

    }  

    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.activity_main, menu);  
        return true;  
    }  

}  



回答3:


On Android it`s called autocompletetextview

Official Google doc: http://developer.android.com/reference/android/widget/AutoCompleteTextView.html

Please check this out,tutorial http://www.tutorialspoint.com/android/android_auto_complete.htm




回答4:


I think what you are looking for can be done with a Spinner instead of an EditText.

Here's a detailed explanation of how to use it.



来源:https://stackoverflow.com/questions/31052436/android-edittext-with-drop-down-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!