如何设置AppCompatSpinner的文本?请参阅详细信息

时间:2016-11-22 15:21:11

标签: java android android-studio datepicker android-spinner

我有一个AppCompatSpinner我在strings.xml文件中保存的条目如下:

<string-array name="startDayList">
        <item>Today</item>
        <item>Tomorrow</item>
        <item>Pick a day..</item>
</string-array>

使用此代码{@ 1}}将其指定为AppCompatSpinner的条目后,AppCompatSpinner上显示的标题或文本为android:entries="@array/startDayList"

此处,当用户选择Today条目时,他会显示Pick a day..并被允许选择日期/日期。以下是(案例2):

DatePicker

此代码会保存在startDaySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { DateFormat currentDateFormat = new SimpleDateFormat("dd-MM-yyyy"); startDayString = currentDateFormat.format(new Date()); switch (i) { case 0: startDayString = currentDateFormat.format(new Date()); break; case 1: Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.DAY_OF_YEAR, 1); Date tomorrow = calendar.getTime(); DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); startDayString = dateFormat.format(tomorrow); break; case 2: final Calendar c = Calendar.getInstance(); mYearStartE = c.get(Calendar.YEAR); mMonthStartE = c.get(Calendar.MONTH); mDayStartE = c.get(Calendar.DAY_OF_MONTH); DatePickerDialog datePickerDialog = new DatePickerDialog(PostSportRequest.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { startDayString = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year; Toast.makeText(getBaseContext(), "startDayString: " + startDayString, Toast.LENGTH_SHORT).show(); } }, mYearStartE, mMonthStartE, mDayStartE); datePickerDialog.show(); break; } } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); 字符串中选择的日期,但startDayString的文字仍需设置为AppCompatSpinner

所以,我想知道如何将微调器的文本/标题设置为用户选择的日期/日期,而不是Pick a day..

请告诉我。

1 个答案:

答案 0 :(得分:1)

在项目视图上设置文字:

    startDaySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(final AdapterView<?> adapterView, View view, int i, long l) {


            DateFormat currentDateFormat = new SimpleDateFormat("dd-MM-yyyy");
            startDayString = currentDateFormat.format(new Date());

            switch (i) {
                case 0:

                    startDayString = currentDateFormat.format(new Date());
                    break;

                case 1:

                    Calendar calendar = Calendar.getInstance();

                    calendar.add(Calendar.DAY_OF_YEAR, 1);
                    Date tomorrow = calendar.getTime();

                    DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
                    startDayString = dateFormat.format(tomorrow);
                    break;

                case 2:
                    final Calendar c = Calendar.getInstance();
                    mYearStartE = c.get(Calendar.YEAR);
                    mMonthStartE = c.get(Calendar.MONTH);
                    mDayStartE = c.get(Calendar.DAY_OF_MONTH);

                    DatePickerDialog datePickerDialog = new DatePickerDialog(PurchaseActivity.this,
                            new DatePickerDialog.OnDateSetListener() {

                                @Override
                                public void onDateSet(DatePicker view, int year,
                                                      int monthOfYear, int dayOfMonth) {

                                    startDayString = dayOfMonth + "-" + (monthOfYear + 1) + "-" + year;

                                    Toast.makeText(getBaseContext(), "startDayString: " + startDayString, Toast.LENGTH_SHORT).show();

                                    TextView mTextView = (TextView) adapterView.getSelectedView();
                                    mTextView.setText(startDayString);
                                }
                            }, mYearStartE, mMonthStartE, mDayStartE);
                    datePickerDialog.show();
                    break;
            }

        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });