我想显示性别选择的下拉列表。我传递了一个字符串数组
String arr[]=new String[]{"male","female"};
但问题是显示默认选择的值为"male"
,我想将"Gender"
显示为默认值。如果我在位置0的数组中传递“Gender”,那么它也会在下拉列表中显示。我只想要“性别”作为提示,但不能在下拉列表中显示。
任何人都可以告诉我如何做到这一点。提前谢谢。
答案 0 :(得分:56)
Spinner sp = (Spinner)findViewById(R.id.spinner);
sp.setSelection(pos);
这里pos是整数(你的数组项位置)
数组如下所示pos = 0;
String str[] = new String{"Select Gender","male", "female" };
然后在onItemSelected
@Override
public void onItemSelected(AdapterView<?> main, View view, int position,
long Id) {
if(position > 0){
// get spinner value
}else{
// show toast select gender
}
}
答案 1 :(得分:1)
我通过扩展ArrayAdapter
并覆盖getView
方法找到了解决方案。
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
/**
* A SpinnerAdapter which does not show the value of the initial selection initially,
* but an initialText.
* To use the spinner with initial selection instead call notifyDataSetChanged().
*/
public class SpinnerAdapterWithInitialText<T> extends ArrayAdapter<T> {
private Context context;
private int resource;
private boolean initialTextWasShown = false;
private String initialText = "Please select";
/**
* Constructor
*
* @param context The current context.
* @param resource The resource ID for a layout file containing a TextView to use when
* instantiating views.
* @param objects The objects to represent in the ListView.
*/
public SpinnerAdapterWithInitialText(@NonNull Context context, int resource, @NonNull T[] objects) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
}
/**
* Returns whether the user has selected a spinner item, or if still the initial text is shown.
* @param spinner The spinner the SpinnerAdapterWithInitialText is assigned to.
* @return true if the user has selected a spinner item, false if not.
*/
public boolean selectionMade(Spinner spinner) {
return !((TextView)spinner.getSelectedView()).getText().toString().equals(initialText);
}
/**
* Returns a TextView with the initialText the first time getView is called.
* So the Spinner has an initialText which does not represent the selected item.
* To use the spinner with initial selection instead call notifyDataSetChanged(),
* after assigning the SpinnerAdapterWithInitialText.
*/
@Override
public View getView(int position, View recycle, ViewGroup container) {
if(initialTextWasShown) {
return super.getView(position, recycle, container);
} else {
initialTextWasShown = true;
LayoutInflater inflater = LayoutInflater.from(context);
final View view = inflater.inflate(resource, container, false);
((TextView) view).setText(initialText);
return view;
}
}
}
在初始化Spinner时,Android会在为T[] objects
中的所有项目调用getView之前调用所选项目的getView。
第一次调用SpinnerAdapterWithInitialText
时,TextView
会返回initialText
super.getView
。
所有其他时间它调用getView
ArrayAdapter
initialText
方法,如果您正常使用微调器,则会调用selectionMade
方法。
要确定用户是否选择了微调器项目,或者微调器是否仍显示No 'Access-Control-Allow-Origin' header
,请调用Access-Control-Allow-Origin
并移交适配器所指定的微调器。
答案 2 :(得分:0)
Spinner不支持Hint,我建议你制作一个自定义微调器适配器。
答案 3 :(得分:-5)
尝试以下:
<Spinner
android:id="@+id/YourSpinnerId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:prompt="Gender" />
答案 4 :(得分:-13)
你可以设置微调器的提示...可以在xml中设置android:prompt =“Gender”