如何在android中的spinner中显示提示/隐藏文本

时间:2013-07-19 11:09:27

标签: android drop-down-menu spinner android-spinner

我需要在微调器(android)中显示提示。

我试过不同的解决方案,但还没有解决问题,那么怎么做呢?

2 个答案:

答案 0 :(得分:0)

我通过使用按钮代替Spinner来处理这个问题。 I have the sample project up on GitHub.

在项目中,我正在显示Spinner和按钮,以显示它们看起来确实相同。除了按钮,您可以将初始文本设置为您想要的任何内容(例如提示)。

这是活动的样子:

package com.stevebergamini.spinnerbutton;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;

public class MainActivity extends Activity {

    Spinner spinner1;
    Button button1;
    AlertDialog ad;
    String[] countries;

    int selected = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        button1 = (Button) findViewById(R.id.button1);

        countries = getResources().getStringArray(R.array.country_names);

        //  You can also use an adapter for the allert dialog if you'd like
        //  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, countries);        

        ad = new AlertDialog.Builder(MainActivity.this).setSingleChoiceItems(countries, selected,  
                new  DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            button1.setText(countries[which]);
                            selected = which;
                            ad.dismiss();

                        }}).setTitle(R.string.select_country).create(); 


        button1.setOnClickListener( new OnClickListener(){

            @Override
            public void onClick(View v) {
                ad.getListView().setSelection(selected);
                ad.show();              
            }});

    }

}

答案 1 :(得分:0)

试试这个:

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), 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("Item 1");
adapter.add("Item 2");
adapter.add("Hint to be displayed");

spinner.setAdapter(adapter);
spinner.setSelection(adapter.getCount()); //displaying hint

来源:Android: Spinner hint

github上的另一个例子:https://github.com/ravivyas84/AndroidSpinnerHint(作者Ravi Vyas)

如果它可以帮助你,你可以使用一点点作弊,你只需将文字颜色改为灰色即可。它可能在许多情况下都不起作用。

希望它有帮助!祝你好运!