在微调器选择上更改微调器promt

时间:2016-04-26 11:46:07

标签: android android-spinner

我有一个带有三个选项的旋转器(“一个”,“两个”“三个”)。当用户选择“一个”时,微调器提示变为“一”。当用户选择“三”时,DatePickerDialog启动,并根据用户输入我想将提示更改为自定义消息。同时我希望微调器保持值“一“,”“两个”“三个”。

有办法吗?

这是我到目前为止所做的,

MainActivity

    spinner=(MySpinner)findViewById(R.id.spinner);
    SpinnerAdapter sa=new SpinnerAdapter(getApplicationContext(), android.R.layout.simple_spinner_item, new String[]{"One", "Two", "Three"});
    spinner.setAdapter(sa);
    spinner.setOnItemSelectedListener(this);

听众

 @Override
 public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
                case 0: var = 0; break;
                case 1: var = 1; break;
                case 2: var = 3; 

                Calendar cal = Calendar.getInstance();
                DatePickerDialog dpd = DatePickerDialog.newInstance(this, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
                dpd.show(getFragmentManager(), "fg");break;
                }

                FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.frame_layout, new FRTransactions()).commit();                          
    }

SpinnerAdapter

public class MySpinner extends Spinner {

int previousPos=-1;

public MySpinner(Context context) {
    super(context);
}

@Override
public void setSelection(int position) {
    super.setSelection(position);

    if (position==getSelectedItemPosition()&&previousPos==position){
        getOnItemSelectedListener().onItemSelected(null, null, position, 0);
    }
    previousPos=position;
}


class SpinnerAdapter extends ArrayAdapter implements Android.widget.SpinnerAdapter {

Context context;
TextView tv;
String[] strings;

public SpinnerAdapter(Context context, int resource, String[] objects) {
    super(context, resource, objects);
    this.context=context;
    this.strings=objects;
}


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

    return makeLayout(position, convertView, parent, android.R.layout.simple_spinner_dropdown_item);
}

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

    int pos=position;

    if (parent instanceof AdapterView)
        pos = ((AdapterView) parent).getSelectedItemPosition();

    return makeLayout(pos, convertView, parent, android.R.layout.simple_spinner_dropdown_item);
}

private View makeLayout(final int position, final View convertView, final ViewGroup parent, final int layout){

    if (convertView!=null) tv=(TextView)convertView;
    else tv=(TextView)LayoutInflater.from(context).inflate(android.R.layout.simple_spinner_dropdown_item, parent, false);

tv.setText(strings[position]);

    return tv;
}

@Override
public int getCount() {
    return strings.length;
}
}

2 个答案:

答案 0 :(得分:0)

嗯,setPrompt()方法怎么样?
您可以在onItemSelected()方法中使用它。

答案 1 :(得分:0)

尝试

new Handler().postDelayed(new Runnable() {
    public void run() {
        spinner.setPrompt("New prompt message");
    }
}, 200);