在 android 工作室中将日期设置为共享首选项
Setting date to a shared prefernece in android studio
android studio 的新手,这是我构建的第一个应用程序,因此我们将不胜感激。我设置了一个日历,我正在尝试将所选日期保存到共享首选项中。我的问题是我创建的方法没有在我的 xml 文件中显示为 onclick 选项。
这是我目前的代码:
public class calendar extends AppCompatActivity {
CalendarView calendarView;
SharedPreferences booking_date;
String myDate, txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);
calendarView = findViewById(R.id.id_calendarView);
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
myDate = dayOfMonth + " " + (month + 1) + " " + year; // note the months start at 0
//redDateText.setText(myDate);
}
});
}
public void goSaveDate(View view){
booking_date = getApplicationContext().getSharedPreferences("booking_details", MODE_PRIVATE);
txt = myDate;
SharedPreferences.Editor editor = booking_date.edit();
editor.putString("key_date", txt);
editor.commit();
Toast.makeText(getBaseContext(), "Date Saved!!", Toast.LENGTH_SHORT).show();
}
public void goShowDate(View view){
booking_date = getApplicationContext().getSharedPreferences("booking_details", MODE_PRIVATE);
String name = booking_date.getString("key_date", null);
TextView textView = findViewById(R.id.textView5);
textView.setText("Your Next booking is on: " +name);
}
@Override
public void finish () {
super.finish();
overridePendingTransition(R.transition.slide_in_left, R.transition.slide_out_right);
}
}
除非您使用 view databinding,否则在 XML 布局中的视图上设置 android:onClick="goSaveDate"
的(旧)方法已被弃用。
Deprecated: View actually traverses the Context hierarchy looking for the relevant method, which is fragile and restricts bytecode optimizers such as R8. Instead, use View.setOnClickListener.
您可以从 activity class 内部设置 OnClickListener,例如在 onCreate 方法内部。
// each click on R.id.button_save_date will call the goSaveDate method
findViewById(R.id.button_save_date).setOnClickListener(this::goSaveDate);
findViewById(R.id.button_show_date).setOnClickListener(this::goShowDate);
将 R.id.button_save_date
和 R.id.button_show_date
替换为您要在其上设置点击侦听器的视图的 ID。如果那些视图(XML 中的按钮、组件)仍然没有在 XML 布局中设置 android:id="@+id/..."
属性,则首先添加它。
语法 ::
(this::goSaveDate
) 是 method reference.
android studio 的新手,这是我构建的第一个应用程序,因此我们将不胜感激。我设置了一个日历,我正在尝试将所选日期保存到共享首选项中。我的问题是我创建的方法没有在我的 xml 文件中显示为 onclick 选项。
这是我目前的代码:
public class calendar extends AppCompatActivity {
CalendarView calendarView;
SharedPreferences booking_date;
String myDate, txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);
calendarView = findViewById(R.id.id_calendarView);
calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
@Override
public void onSelectedDayChange(@NonNull CalendarView view, int year, int month, int dayOfMonth) {
myDate = dayOfMonth + " " + (month + 1) + " " + year; // note the months start at 0
//redDateText.setText(myDate);
}
});
}
public void goSaveDate(View view){
booking_date = getApplicationContext().getSharedPreferences("booking_details", MODE_PRIVATE);
txt = myDate;
SharedPreferences.Editor editor = booking_date.edit();
editor.putString("key_date", txt);
editor.commit();
Toast.makeText(getBaseContext(), "Date Saved!!", Toast.LENGTH_SHORT).show();
}
public void goShowDate(View view){
booking_date = getApplicationContext().getSharedPreferences("booking_details", MODE_PRIVATE);
String name = booking_date.getString("key_date", null);
TextView textView = findViewById(R.id.textView5);
textView.setText("Your Next booking is on: " +name);
}
@Override
public void finish () {
super.finish();
overridePendingTransition(R.transition.slide_in_left, R.transition.slide_out_right);
}
}
除非您使用 view databinding,否则在 XML 布局中的视图上设置 android:onClick="goSaveDate"
的(旧)方法已被弃用。
Deprecated: View actually traverses the Context hierarchy looking for the relevant method, which is fragile and restricts bytecode optimizers such as R8. Instead, use View.setOnClickListener.
您可以从 activity class 内部设置 OnClickListener,例如在 onCreate 方法内部。
// each click on R.id.button_save_date will call the goSaveDate method
findViewById(R.id.button_save_date).setOnClickListener(this::goSaveDate);
findViewById(R.id.button_show_date).setOnClickListener(this::goShowDate);
将 R.id.button_save_date
和 R.id.button_show_date
替换为您要在其上设置点击侦听器的视图的 ID。如果那些视图(XML 中的按钮、组件)仍然没有在 XML 布局中设置 android:id="@+id/..."
属性,则首先添加它。
语法 ::
(this::goSaveDate
) 是 method reference.