Android的微调器设置默认的文本

Android的微调器设置默认的文本

本文介绍了Android的微调器设置默认的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个微调到我的应用程序。我展示的学校的名单,但我希望有一个默认值/提示,以便用户知道选择一所学校。我也想这样做,而不必有提示的列表项。任何想法?

下面是我的飞旋code迄今:

  selectSchoolSpinner =(微调)findViewById(R.id.select_school_spinner);
spinnerSchoolList =新的ArrayList<串GT;();spinnerSchoolList.add(请选择学校);
//我不希望有一个像后者提示ArrayAdapter<串GT; spinnerAdapter =新ArrayAdapter<串GT;(这一点,
    android.R.layout.simple_spinner_item,spinnerSchoolList);spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);selectSchoolSpinner.setAdapter(spinnerAdapter);


解决方案

您应该创建自定义ArrayAdapter为在其构造所需的文本字符串。然后,你应该用默认的文本转换你的数组的第一个元素,并在微调的用户点击后,应该切换回原来的阵列。

例如:

活动类:

 公共类SpinnerActivity延伸活动{    微调微调;
    字符串defaultTextForSpinner =你的deafult的文字在这里;
    的String [] = arrayForSpinner {一,二,三};    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        的setContentView(R.layout.activity_spinner);        微调=(微调)findViewById(R.id.your_spinner);
        spinner.setAdapter(新CustomSpinnerAdapter(这一点,R.layout.spinner_row,arrayForSpinner,defaultTextForSpinner));
    }
    }

CustomSpinnerAdapter类:

 公共类CustomSpinnerAdapter扩展ArrayAdapter<串GT; {    上下文语境;
    的String []对象;
    串firstElement;
    布尔isFirstTime;    公共CustomSpinnerAdapter(上下文的背景下,INT textViewResourceId,字符串[]对象,字符串defaultText){
        超(背景下,textViewResourceId,对象);
        this.context =背景;
        this.objects =物体;
        this.isFirstTime =真;
        setDefaultText(defaultText);
    }    @覆盖
    公共查看getDropDownView(INT位置,查看convertView,父母的ViewGroup){
        如果(isFirstTime){
            对象[0]​​ = firstElement;
            isFirstTime = FALSE;
        }
        返回getCustomView(位置,convertView,父母);
    }    @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup){
        notifyDataSetChanged();
        返回getCustomView(位置,convertView,父母);
    }    公共无效setDefaultText(字符串defaultText){
        this.firstElement =对象[0]​​;
        对象[0]​​ = defaultText;
    }    公共查看getCustomView(INT位置,查看convertView,父母的ViewGroup){        LayoutInflater吹气=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        查看排= inflater.inflate(R.layout.spinner_row,父母,假);
        TextView的标签=(TextView中)row.findViewById(R.id.spinner_text);
        label.setText(对象[位置]);        返回行;
    }    }

I've implemented a spinner into my app. I'm showing a list of schools but I want a default value/prompt so the user knows to select a school. I also want to do this without having to have the prompt as a list item. Any ideas?

Here is my spinner code so far:

selectSchoolSpinner = (Spinner) findViewById(R.id.select_school_spinner);
spinnerSchoolList = new ArrayList<String>();

spinnerSchoolList.add("Please select School");
//I don't want to have a prompt like the latter

ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, spinnerSchoolList);

spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

selectSchoolSpinner.setAdapter(spinnerAdapter);
解决方案

You should create custom ArrayAdapter with a String for your desired text in its constructor. Then, you should switch the first element of your array with your default text, and after the user click on the spinner it should switch back to the original array.

Example:

Activity class:

    public class SpinnerActivity extends Activity {

    Spinner spinner;
    String defaultTextForSpinner = "Your deafult text here";
    String[] arrayForSpinner = {"One", "Two", "Three"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_spinner);

        spinner = (Spinner) findViewById(R.id.your_spinner);
        spinner.setAdapter(new CustomSpinnerAdapter(this, R.layout.spinner_row, arrayForSpinner, defaultTextForSpinner));
    }
    }

CustomSpinnerAdapter class:

    public class CustomSpinnerAdapter extends ArrayAdapter<String>{

    Context context;
    String[] objects;
    String firstElement;
    boolean isFirstTime;

    public CustomSpinnerAdapter(Context context, int textViewResourceId, String[] objects, String defaultText) {
        super(context, textViewResourceId, objects);
        this.context = context;
        this.objects = objects;
        this.isFirstTime = true;
        setDefaultText(defaultText);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if(isFirstTime) {
            objects[0] = firstElement;
            isFirstTime = false;
        }
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        notifyDataSetChanged();
        return getCustomView(position, convertView, parent);
    }

    public void setDefaultText(String defaultText) {
        this.firstElement = objects[0];
        objects[0] = defaultText;
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.spinner_row, parent, false);
        TextView label = (TextView) row.findViewById(R.id.spinner_text);
        label.setText(objects[position]);

        return row;
    }

    }

这篇关于Android的微调器设置默认的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 02:45