微调器提示不会显示

时间:2017-04-25 03:37:12

标签: android spinner prompt

我想使用微调器提示,例如"选择城市"。我在布局select_city中的字符串布局使用中设置了android:prompt="@string/select_city"的值,但它没有用。也尝试使用sp_city.setPrompt("Select City");也没有用。

我的问题是什么?我该如何解决它并设置提示?

布局:

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

}

3 个答案:

答案 0 :(得分:2)

你可以这样做,我已经做了一些棘手的方式。它可以帮助你

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

您错误地提示默认文字。 点击微调器,您将看到&#34;选择City&#34;作为标题

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

如果要在微调器上选择默认值  当您没有从微调器下拉列表中选择任何值时。 然后你必须使用

  

NothingSelectedSpinnerAdapter

How to make an Android Spinner with initial text "Select One"