使用微调器设置日期 - Android

时间:2017-07-04 11:50:57

标签: android date spinner

我想要的是,当我在微调器上选择"Today""Tomorrow"时,会在date format editText中显示它。 "Pick a date"正在运行,但我只能选择一次日期,所以我也需要修复它。这是代码:

package com.example.android.myapplication;

import android.app.DatePickerDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import java.util.Calendar;

public class MainActivity extends AppCompatActivity {
Spinner spinner;
ArrayAdapter<CharSequence> adapter;

private static final String TAG = "MainActivity";

private EditText editText;
private DatePickerDialog.OnDateSetListener mDateSetListener;

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

    editText = (EditText) findViewById(R.id.editText);

    spinner = (Spinner) findViewById(R.id.spinner);
    adapter = (ArrayAdapter.createFromResource(this, R.array.dates, android.R.layout.simple_spinner_item));
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            switch (spinner.getSelectedItem().toString()) {
                case "Pick a date":
                    Calendar cal = Calendar.getInstance();
                    int year = cal.get(Calendar.YEAR);
                    int month = cal.get(Calendar.MONTH);
                    int day = cal.get(Calendar.DAY_OF_MONTH);

                    DatePickerDialog dialog = new DatePickerDialog(
                            MainActivity.this,
                            android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                            mDateSetListener,
                            year,month,day);
                    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    dialog.show();

                case "Today":
                    //set today's date
                case "Tomorrow":
                    //set tomorrow's date
            }
        }

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

        }
    });

    mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {
            month = month + 1;
            Log.d(TAG, "onDateSet: mm/dd/yyy: " + month + "/" + day + "/" + year);

            String date = month + "/" + day + "/" + year;
            editText.setText(date);
        }
    };

}

2 个答案:

答案 0 :(得分:0)

我设法为"Today"做了,但我明天需要帮助..

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);

        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        String formattedDate = df.format(cal.getTime());


        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            switch (spinner.getSelectedItem().toString()) {
                case "Pick a date":

                    DatePickerDialog dialog = new DatePickerDialog(
                            MainActivity.this,
                            android.R.style.Theme_Holo_Light_Dialog_MinWidth,
                            mDateSetListener,
                            year,month,day);

                    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                    dialog.show();

                case "Today":
                    editText.setText(formattedDate);
                case "Tomorrow":

            }
        }

答案 1 :(得分:0)

明天只需更改一件简单的事情:

case "Tomorrow":
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH,1);
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        int day = cal.get(Calendar.DAY_OF_MONTH);

        SimpleDateFormat df = new SimpleDateFormat("dd/MM/yyyy");
        String formattedDate = df.format(cal.getTime());

        editText.setText(formattedDate);

希望它有所帮助!!!