使用弹出日历在另一个活动中显示所选日期

时间:2017-06-08 05:35:09

标签: android android-activity calendar

我想在另一个活动中显示日历中的所选日期,所以请帮助我,现在在我的应用程序中它显示当前日期但我不知道如何做到这一点。我在我的应用程序中使用简单的弹出日历。这是我的代码。

    sharedpreferences = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);

    Bundle b = getIntent().getExtras();

    txt_date = (TextView) findViewById(R.id.Date_Value);
    txt_week = (TextView) findViewById(R.id.Week_Value);
    txt_baby = (TextView) findViewById(R.id.Baby_Value);
    txt_mother = (TextView) findViewById(R.id.Mother_Value);
    txt_tremester = (TextView) findViewById(R.id.Tremester_Value);

    txt_baby.setText(b.getCharSequence("baby_name"));
    txt_mother.setText(b.getCharSequence("mother_name"));
    txt_tremester.setText(b.getCharSequence("tremester"));

    img_week = (ImageView) findViewById(R.id.image_week);
    img_slide = (ImageView) findViewById(R.id.image_slide);
    img_BMI = (ImageView) findViewById(R.id.image_BMI);
    img_setting = (ImageView) findViewById(R.id.image_setting);

    String dob = sharedpreferences.getString("dob", null);

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



    txt_date.setText(new StringBuilder()
            // Month is 0 based, just add 1
            .append(year).append("-").append(month + 1).append("-").append(day));

    String current_dob = txt_date.getText().toString();

    StringTokenizer current_tokens = new StringTokenizer(current_dob, "-");
    current_year = current_tokens.nextToken();
    current_month = current_tokens.nextToken();
    current_date = current_tokens.nextToken();

    System.out.println("year==>" + Integer.parseInt(current_year));
    System.out.println("month==>" + Integer.parseInt(current_month));
    System.out.println("date==>" + Integer.parseInt(current_date));

    StringTokenizer tokens = new StringTokenizer(dob, "-");
    selected_year = tokens.nextToken();
    selected_month = tokens.nextToken();
    selected_date = tokens.nextToken();

    System.out.println("year==>" + Integer.parseInt(selected_year));
    System.out.println("month==>" + Integer.parseInt(selected_month));
    System.out.println("date==>" + Integer.parseInt(selected_date));

1 个答案:

答案 0 :(得分:0)

使用日历视图并按如下所示设置所选日期:

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, day);

long milliTime = calendar.getTimeInMillis(); 

mCalendarView.setDate (milliTime, true, true);