Calender.getInstance() 在 Android Studio 中给出错误

我试图在单击 my 时弹出一个对话框。问题是,当我尝试用令人毛骨悚然的红色下划线实例化我的对象时,下面会出现。DatePickerEditTextCalenderCalender.getInstance()getInstance()

Calender myCalender = Calender.getInstance();

将鼠标悬停在它上面,它说

调用需要 API 级别 24(当前最小值为 15)。

显然,我希望我的应用在最低 API 级别 15 的设备上运行。请帮帮我这个,我是Android的新手。谢谢。

我试图按照这种方法将日期收集器添加到我的编辑文本


答案 1

您有错误的语句。它需要 .我的猜测是,你有一个.importCalendarjava.util.Calendarimportandroid.icu.util.Calendar


答案 2
This code will help  you to solve your problem..
It will pop-open a DatePicker dialogue box when onClick EditText box:

1) MainActivity.java

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.icu.text.DateFormat;
import android.icu.util.Calendar; 
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity
{
    EditText editText1;
    private int nYear,nMonth,nDay,sYear,sMonth,sDay;
    static final int DATE_ID = 0;
    Calendar c = Calendar.getInstance();

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

        editText1 = (EditText) findViewById(R.id.editText1);
        sYear = c.get(Calendar.YEAR);
        sMonth = c.get(Calendar.MONTH);
        sDay = c.get(Calendar.DAY_OF_MONTH);
        //editText
        editText1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog(DATE_ID);
            }
        });

    /***************calender code*****************/
    private void calender_fetch()
    {
        editText1.setText((nMonth+1)+"/"+nDay+"/"+nYear+"");
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth)
        {
            nYear = year;
            nMonth = monthOfYear;
            nDay = dayOfMonth;
            calender_fetch();
        }
    };
    @Override
    protected Dialog onCreateDialog(int id)
    {
        switch (id)
        {
            case DATE_ID:
                return new DatePickerDialog(this, mDateSetListener, sYear, sMonth, sDay);
        }
        return null;
    }
    /*************************calender close*********************************/

推荐