如何使用库材料日历视图保存用户每天在EditText中输入的内容?

时间:2017-06-16 14:12:50

标签: java android android-studio calendar

我正在使用图书馆资料日历视图实施日历应用。该应用程序是一个日记,用户可以在其中选择日期,在那里他可以更改背景图像,从图库中放置图像或制作照片和表情符号雨以获得乐趣。我的问题是如何在每一天保存“标题”和“描述”(editText)?当用户关闭应用程序然后再次打开它时,我希望输入的文本位于用户输入其信息的特定日期。

我尝试使用“共享首选项”来保存标题和说明,但信息是在整个日历日保存的,我不知道如何在每个日期保存。是否有材料日历视图的解决方案? 我将数据保存到一个包中,然后将其传递给活动“Days”,在这里我想在白天保存标题和描述。

活动代码“日历”

   protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_calendars);

        MaterialCalendarView materialCalendarView = (MaterialCalendarView) findViewById(calendarView);
      materialCalendarView.state().edit()
                .setFirstDayOfWeek(Calendar.MONDAY)
                .setMinimumDate(CalendarDay.from(1900, 1, 1))
                .setMaximumDate(CalendarDay.from(2100, 12, 31))
                .setCalendarDisplayMode(CalendarMode.MONTHS)
                .commit();
        //set the current day to be highlighted
        Calendar calendar = Calendar.getInstance();
        materialCalendarView.setDateSelected(calendar.getTime(), true);

        materialCalendarView.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
                //Toast.makeText(Calendars.this, " " + date, Toast.LENGTH_LONG).show();
                Intent intentBundle = new Intent(Calendars.this, Days.class);
                Bundle bundle = new Bundle();
                bundle.putString("Data", String.valueOf(date));
                intentBundle.putExtras(bundle);
                startActivity(intentBundle);
            }
        });


    }

活动日的代码,我想在白天保存标题和说明:

private EditText txtTitle;
    private EditText txt_description;
    private Button btnSave;
    private EditText txtDate;

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_days);

        //for saving data initialize the views
        btnSave = (Button) findViewById(R.id.btnSave);
        txtTitle = (EditText) findViewById(R.id.txtTitle);
        txt_description = (EditText) findViewById(R.id.txt_description);
        //adding click event on button save
        btnSave.setOnClickListener(this);
        loadSavedPreferences();
       //get bundle open
        Bundle bundle = getIntent().getExtras();
        //Extract the data
        String data = bundle.getString("Data");
        //Create the text wiew
        TextView textView = (TextView) this.findViewById(R.id.txtDate);
        textView.setTextSize(17);
        textView.setText(data);
}

//for saving data
    private void loadSavedPreferences(){
        SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
        String title = sharedPreferences.getString("storedTitle",  "Enter title");
        String description = sharedPreferences.getString("storedDescription", "Enter description");
        txtTitle.setText(title);
        txt_description.setText(description);


    }
    private void savePreferences(String key, String value){
        SharedPreferences sharedPreferences= PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key,value);

        editor.apply();
    }

按钮保存代码

case R.id.btnSave:
                savePreferences("storedTitle", txtTitle.getText().toString());
                savePreferences("storedDescription", txt_description.getText().toString());
                 Toast.makeText(Days.this,
                "Data was saved!", Toast.LENGTH_SHORT)
                 .show();

1 个答案:

答案 0 :(得分:0)

您可以使用数据库,例如,包含日期和标题的对象以及您要保存的任何其他内容。有一些很好的ORM,比如SugarORM,DBFlow,Realm和这样的