2015-01-16 4 views
0

표시된 Spinner를 구현하는 작업이 있습니다. 드롭 다운에서 전체 이름을 가진 국가를 선택하면 해당 국가 코드 (GB, AU ...)가 선택된 Spinner 항목. 나는 이런 식으로 구현하는 방법을 모른다. 그것을하기의 다만 약간 암시는 중대 할 것이다. 모두에게 감사하십시오. string.xml에서드롭 다운 메뉴에서 맞춤 Spinner 구현을 선택하십시오.

enter image description here

+1

당신은'Spinner'를 사용하여이 작업을 할 수 없습니다 (http://stackoverflow.com/questions/21329132/android-custom- dropdown-popup-menu) 또는이 [library] (https://github.com/lorensiuswlt/NewQuickAction3D) –

+0

@Sharp edge를 살펴보면 팝업 메뉴로 만들 것입니다. 힌트를 가져 주셔서 감사합니다. –

+0

정확히 같은 * pointy 화살표 * 메뉴를 원한다고 가정합니다. 이전 커멘트에서 언급 한 커스텀 라이브러리를 사용해야 만하기 때문에 간단한 항목 목록의 경우'Spinner'로 충분합니다. –

답변

0

먼저

<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

또는

같은 몇 가지 라이브러리를 사용 중 하나가 당신은 주변에 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

그는 팝업 상단에 뾰족한 화살을 올려 놓기를 원합니다! 너는 '회 전자'로 그걸 가질 수 없다. –

+0

그래, 나는 오해했을거야. 하지만 네, 동의합니다. Spinner를 그렇게 보이게 만들 수는 없습니다. – torque203

관련 문제