java - 如何使用库“ Material 日历 View ”保存用户每天在EditText中输入的内容?

标签 java android android-studio calendar

我正在使用库Material Calendar View实现日历应用程序。该应用程序是一本日记,用户可以在其中选择日期,在那里他可以更改背景图像,从图库中放置图像或制作照片和表情符号雨来娱乐。我的问题是如何每天保存“标题”和“描述”(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();

最佳答案

您可以为其使用数据库,例如,一个包含日期和标题以及您要保存的其他任何东西的对象。有一些很好的ORM,例如SugarORM,DBFlow,Realm等

关于java - 如何使用库“ Material 日历 View ”保存用户每天在EditText中输入的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44591471/

相关文章:

android-studio - IntelliJ IDEA 和 Atom 编辑器中的相同键盘映射

java - 如何在java中耳机连接到手机时启动应用程序

java - MediaStore.Images.Media.getBitmap 意外失败

java - 什么是单向多对多关联?

java - Eclipse 中的 JiBX 绑定(bind) DTD 模式

java - Dagger 2 生成代码中 proxyProvide 的用途

javascript - Webview 不加载 javascript

java - 即使类的字段尚未初始化,Java 是否也会为类的字段分配内存?

java - 为什么 IntelliJ 给我 "Package doesn' t 存在“错误?

android - 如何在 Xamarin .axml for Android 中应用自定义 View