DatePicker Java / Android Studio中的日期突出显示

时间:2017-03-06 10:46:21

标签: java android

我是Java和Android Studio的超级新手。我已经启动了具有日期功能的应用程序。我在应用程序菜单中创建了3个按钮。一个是按钮日期按钮前进

案例1。当您启动应用时,当前日期显示日期按钮,当您单击按钮后退或向前时,中间按钮上的日期会相应更改一天。

案例2。当您单击日期按钮本身(第一次)时,它会弹出日期选择器,并突出显示按钮中的日期,您可以更改日期并更改日期按钮相应地从选择器。

案例3。但是当我在选择器退出后前后使用按钮时(一次)按钮上的日期仍会发生变化,但它在选择器中没有突出显示。它只是第一次正确突出显示,它也通过选择器更改日期时正确突出显示,但不通过后退和前进按钮突出显示。

我做错了什么(除了使用弃用的方法?;-))。任何意见都将非常感激。

//variables for date

int year_x,month_x,day_x;

DateFormat df = new SimpleDateFormat("dd-MM-yyyy");

MenuItem dynamicDate;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);


    dynamicDate = menu.findItem(R.id.action_date);
    Calendar cal = Calendar.getInstance();
    year_x = cal.get(Calendar.YEAR);
    month_x = cal.get(Calendar.MONTH) + 1;
    day_x = cal.get(Calendar.DAY_OF_MONTH);

    String currentDateString = (day_x<10?("0"+day_x):(day_x)) + "-" + (month_x<10?("0"+month_x):(month_x)) + "-" + year_x;
    Date currentDate = null;
    try {
        currentDate = df.parse(currentDateString);
    } catch (ParseException e) {
        e.printStackTrace();
    }
    dynamicDate.setTitle(currentDateString);


    return true;
}

public DatePickerDialog.OnDateSetListener myDateListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker arg0, int year_xk, int month_xk, int day_xk) {


        Calendar cal = Calendar.getInstance();
        cal.set(year_xk, month_xk, day_xk);
        Date dateAfterChange = cal.getTime();
        String dateAfterChangeString = df.format(dateAfterChange);
        dynamicDate.setTitle(dateAfterChangeString);
        //Toast.makeText(MainActivity.this, "you clicked date", Toast.LENGTH_SHORT).show();
    }
};


@Override
public Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
    if (id == 0473) {
        String currentDateString =  dynamicDate.getTitle().toString();
        //DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        Date currentDate = null;
        try {
            currentDate = df.parse(currentDateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(currentDate);



        return new DatePickerDialog(this, myDateListener, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH));
    }
    return null;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement


    if (id == R.id.action_back) {
        String currentDateString =  dynamicDate.getTitle().toString();
        //DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        Date currentDate = null;
        try {
            currentDate = df.parse(currentDateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(currentDate);
        cal.add(Calendar.DATE, -1);

        Date dateDayBefore = cal.getTime();
        String dateDayBeforeString = df.format(dateDayBefore);
        dynamicDate.setTitle(dateDayBeforeString);
        //return true;
    }



    if (id == R.id.action_date) {



        showDialog(0473);




    }

    if (id == R.id.action_forward) {
        String currentDateString =  dynamicDate.getTitle().toString();
        DateFormat df = new SimpleDateFormat("dd-MM-yyyy");
        Date currentDate = null;
        try {
            currentDate = df.parse(currentDateString);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(currentDate);
        cal.add(Calendar.DATE, +1);


        Date dateDayAfter = cal.getTime();
        String dateDayAfterString = df.format(dateDayAfter);
        dynamicDate.setTitle(dateDayAfterString);
        //return true;
    }



    return super.onOptionsItemSelected(item);
}

1 个答案:

答案 0 :(得分:0)

我曾经使用removeDialog(当按下后退和前进按钮时)onCreateDialog在每次按下日期时都能正常工作。