2015-06-25 46 views
7

我有一個edittext從用戶獲取值。我想添加一個選項,當點擊edittext時,用戶可以通過下拉列表從不同的選項中進行選擇。有沒有人有一個想法如何做到這一點?安卓edittext與下拉列表

這是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

+0

在android –

回答

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; 
    } 

} 
+0

中被稱爲AutoCompleteTextView在列表視圖中沒有它的非autocompletetextview只有選定的值有 –

+0

抱歉,沒有得到你。請詳細說明。 –

+0

檢查我編輯的問題 –

15

我覺得這是你的要求:

只要按照這個例子: 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> 

活動: -

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

+0

我只是不想輸入編輯文本我想從列表中獲得價值當我點擊編輯文本此列表將打開.is任何方式來做到這一點 –

+0

這也是像你一樣要求權利? – RaMeSh

+0

首先你的日期選擇器不是一個edittext,你爲什麼要編輯文本下拉。 – RaMeSh