Android 微调器提示不';我没有出现

Android 微调器提示不';我没有出现,android,spinner,prompt,Android,Spinner,Prompt,我想使用微调器提示,如“选择城市”。我确实为字符串布局中的select_city设置了一个值,在布局中使用android:prompt=“@string/select_city”,但它不起作用。还尝试使用sp_city.setPrompt(“选择城市”)也不起作用 我有什么问题?如何修复它并设置提示 布局: <Spinner android:id="@+id/spinner_poi_city" android:layout_width="match_parent"

我想使用微调器提示,如“选择城市”。我确实为字符串布局中的
select_city
设置了一个值,在布局中使用
android:prompt=“@string/select_city”
,但它不起作用。还尝试使用
sp_city.setPrompt(“选择城市”)也不起作用

我有什么问题?如何修复它并设置提示

布局:

<Spinner
    android:id="@+id/spinner_poi_city"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/btn_dropdown"
    android:spinnerMode="dropdown"/>

类别:

public class FirstPOIPreference extends DialogPreference {

    private Spinner sp_city;
    private ArrayAdapter<String> adapter;
    private POI poiDialog = new POI();

    public FirstPOIPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setPersistent(false);
        setDialogLayoutResource(R.layout.pref_poi_dialog);
        setPositiveButtonText(android.R.string.ok);
        setNegativeButtonText(android.R.string.cancel);
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        initViews(view);
    }

    private void initViews(View view) {
        sp_city = (Spinner) view.findViewById(R.id.spinner_poi_city);
        sp_city.setPrompt("City");
        String[] arrayCity = new String[]{"Erie", "Pittsburgh", "Cleveland", "Buffalo", "Niagara Falls", "Jamestown"};
        adapter = new ArrayAdapter <> (this.getContext(), android.R.layout.simple_spinner_dropdown_item, arrayCity);
        sp_city.setAdapter(adapter);

        sp_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView <?> parentView, View selectedItemView, int position, long id) {
                poiDialog.setCity(sp_city.getSelectedItem().toString());
            }

            @Override
            public void onNothingSelected(AdapterView <?> parentView) {
            }
        });
    }

}
public类首选项扩展了DialogPreference{
私人纺纱厂;
专用阵列适配器;
私有POI对话框=新POI();
公共首选项(上下文、属性集属性){
超级(上下文,attrs);
init();
}
私有void init(){
setPersistent(假);
setDialogLayoutResource(R.layout.pref_poi_对话框);
setPositiveButtonText(android.R.string.ok);
setNegativeButtonText(android.R.string.cancel);
}
@凌驾
受保护的void onBindDialogView(视图){
super.onBindDialogView(视图);
初始视图(视图);
}
私有void initViews(视图){
sp_city=(微调器)view.findViewById(R.id.Spinner_poi_city);
sp_city.setPrompt(“城市”);
字符串[]arrayCity=新字符串[]{“伊利”、“匹兹堡”、“克利夫兰”、“水牛城”、“尼亚加拉瀑布”、“詹姆斯敦”};
adapter=new ArrayAdapter(this.getContext(),android.R.layout.simple\u微调器\u下拉菜单\u项,arrayCity);
sp_city.setAdapter(适配器);
sp_city.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图selectedItemView、整型位置、长id){
poidiol.setCity(sp_city.getSelectedItem().toString());
}
@凌驾
未选择公共无效(AdapterView父视图){
}
});
}
}

我认为更好的解决方案是使用“选择城市”作为将添加到微调器的数组的第一项。因此,默认情况下,它将显示“选择城市”。通过检查微调器的“选定位置”,可以检查是否选择了城市。如果位置为0,则显示toast或类似“请选择城市”的内容。

您可以这样做,我用了一些巧妙的方法。这对您有帮助

private void initViews(View view) {

sp_city = (Spinner) view.findViewById(R.id.spinner_poi_city);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item) {

                @Override
                public View getView(int position, View convertView, ViewGroup parent) {

                    View v = super.getView(position, convertView, parent);
                    if (position == getCount()) {
                        ((TextView) v.findViewById(android.R.id.text1)).setText("");
                        ((TextView) v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); //"Hint to be displayed"
                    }
                    return v;
                }

                @Override
                public int getCount() {
                    return super.getCount() - 1; // you dont display last item. It is used as hint.
                }
            };
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            adapter.add("Erie");
            adapter.add("Pittsburgh");
            adapter.add("Cleveland");
            adapter.add("Buffalo");
            adapter.add("Niagara Falls");
            adapter.add("Jamestown");
            adapter.add("Select City"); //This is the text that will be displayed as hint.

            sp_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                    String spinnerValue = String.valueOf(sp_city.getSelectedItem().toString());
                    if (!spinnerValue.equalsIgnoreCase("Select City")) {
                        //do your code
                        Toast.makeText(this, sp_city.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
                    }
                }
                @Override
                public void onNothingSelected(AdapterView<?> parentView) {
                }
            });

            sp_city.setAdapter(adapter);
            sp_city.setSelection(adapter.getCount()); //set the hint the default selection so it appears on launch.
        }
private void initViews(视图){
sp_city=(微调器)view.findViewById(R.id.Spinner_poi_city);
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u微调器\u下拉菜单\u项){
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=super.getView(位置、转换视图、父级);
if(position==getCount()){
((TextView)v.findViewById(android.R.id.text1)).setText(“”);
((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount());/“要显示的提示”
}
返回v;
}
@凌驾
public int getCount(){
return super.getCount()-1;//不显示最后一项。它用作提示。
}
};
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
适配器。添加(“伊利”);
适配器。添加(“匹兹堡”);
适配器。添加(“克利夫兰”);
适配器。添加(“水牛”);
适配器。添加(“尼亚加拉瀑布”);
适配器。添加(“詹姆斯敦”);
adapter.add(“Select City”);//这是将显示为提示的文本。
sp_city.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图selectedItemView、整型位置、长id){
String spinnerValue=String.valueOf(sp_city.getSelectedItem().toString());
如果(!spinnerValue.equalsIgnoreCase(“选择城市”)){
//做你的代码
Toast.makeText(this,sp_city.getSelectedItem().toString(),Toast.LENGTH_LONG.show();
}
}
@凌驾
未选择公共无效(AdapterView父视图){
}
});
sp_city.setAdapter(适配器);
sp_city.setSelection(adapter.getCount());//将提示设置为默认选择,使其在启动时显示。
}

您将提示误认为默认文本。 点击微调器,您将看到“选择城市”作为标题

提示用于在下拉式弹出窗口中显示标题,而不是默认文本

如果要在微调器上选择默认值 未从微调器下拉列表中选择任何值时。 那么你必须使用

无选择的旋转适配器


您知道android spinner中的微调器提示是什么吗?我想你实际上把它误认为是旋转器的第一个元素!若你们可以这样做,微调器有一个额外的值显示,如“选择城市”。当你们显示土司,如“请选择城市”。默认情况下,每次都会以初始级别显示。否。设置适配器后,您可以使用setOnItemClickListener。第二,你不需要设置项目点击监听器,只需要在执行某些操作之前通过GetSlectEditePosition获取微调器位置。是的,我知道,但我担心第一个值选择城市总是在微调器列表中。这是显示用户的错误方式。这是最简单的方法。可以通过android.R.id.text1的自定义适配器来完成?