2015-01-16 122 views
0

我有一个任务来实现显示的微调,当你从下拉菜单的国家代码(GB,AU ...)中选择具有全名的国家时,必须在选定的微调项目。我不知道如何像这样实现它。只是做一些提示会很好。关心所有。在下拉菜单中选择

enter image description here

+1

使用'Spinner'你不能让这个但是你可以使用一些东西一样[弹出菜单](http://stackoverflow.com/questions/21329132/android-custom-下拉菜单)或者看看这个[库](https://github.com/lorensiuswlt/NewQuickAction3D) –

+0

@Sharp边缘,我会弹出菜单。感谢提示。 –

+0

我假设你想要完全相同的*尖箭头*菜单,因为你必须使用我在之前的评论中提到的一些自定义库,但对于简单的项目列表,“微调”就足够了。 –

回答

0

string.xml首先声明的所有国家名单中排列

<string-array name="countries"> 
    <item>India</item> 
    <item>Australia</item> 
    <item>England</item> 
    <item>Pakistan</item> 
</string-array> 

然后声明微调布局

<Spinner 
    android:id="@+id/spinnerPlayerType" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:entries="@array/countries"   
    android:focusable="false" /> 
1

您不能完全相同的尖尖弹出菜单使用Spinner! 你必须要么使用一些像图书馆this

OR

你可以扩展PopupWindow类和食堂与它周围。

1

为此,您需要有一个自定义的微调控制器适配器和一个自定义类来容纳这两个变量。

创建一个类,其中包含您将显示的每个项目的名称和国家代码。

事情是这样的:

public class Country { 
    public name; 
    public code; 
} 

使用您选择的微调的适配器。我会推荐BaseAdapter

覆盖适配器上的getViewgetDropdownView。所有适配器都有这些方法。

getView方法将确定在微调器关闭后显示的内容,因此在这里您将TextView的文本设置为所选项目的国家代码。

在getDropDownView上,您将根据每个选项的位置将您显示的国家/地区的名称设置为文本。

下面你可以找到一个最小的适配器,它将完成我上面描述的任务。

import android.content.Context; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 

import java.util.ArrayList; 
import java.util.List; 

public class CountryAdapter extends BaseAdapter { 

private List<Country> countryList; 

public CountryAdapter() { 
    //Initialize the list however you need to. 
    countryList = new ArrayList<>(); 
} 

@Override 
public int getCount() { 
    return countryList.size(); 
} 

@Override 
public Object getItem(int position) { 
    return countryList.get(position); 
} 

@Override 
public long getItemId(int position) { 
    return 0; 
} 

@Override 
public boolean hasStableIds() { 
    return false; 
} 

@Override 
public View getView(int position, View view, ViewGroup parent) { 
    Context context = parent.getContext(); 
    if (view == null || !view.getTag().toString().equals("NON_DROPDOWN")) { 
     view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.spinner_item, parent, false); 
     view.setTag("NON_DROPDOWN"); 
    } 

    String countryCode = countryList.get(position).code; 
    //Here you can set the label to the country code. 

    return view; 
} 

@Override 
public View getDropDownView(int position, View view, ViewGroup parent) { 
    Context context = parent.getContext(); 
    if (view == null || !view.getTag().toString().equals("DROPDOWN")) { 
     view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.spinner_item_dropdown, 
       parent, false); 
     view.setTag("DROPDOWN"); 
    } 

    String countryName = countryList.get(position).name; 
    //Here you set the text of your label to the name of the country. 

    return view; 
} 

private class Country { 

    public String name; 
    public String code; 
} 

}

+0

他想要弹出顶部的尖箭头事物!你不能通过'Spinner'获得那个 –

+0

是的,我一定误会了。但是,我同意,你不能让Spinner看起来像那样。 – torque203