英文:
How can I get the date from MaterialDatePicker?
问题
我的应用程序使用MaterialDatePicker。我想创建一个类,其中将存储来自MaterialDatePicker的日期,以及其他数据。
我有一些问题:
- 如果日期可以更改,最佳的保存日期的方式是什么?
- 如何从MaterialDatePicker获取日期?
以下是我的具有MaterialDatePicker的活动代码:
public class NewEvent extends AppCompatActivity {
// ...(你的代码)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_event);
// ...(你的代码)
// MaterialDatePicker的部分
MaterialDatePicker.Builder builder = MaterialDatePicker.Builder.datePicker();
builder.setTitleText("选择日期");
final MaterialDatePicker materialDatePicker = builder.build();
etDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
materialDatePicker.show(getSupportFragmentManager(), "DATE_PICKER");
}
});
materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
@Override
public void onPositiveButtonClick(Object selection) {
etDate.setText(materialDatePicker.getHeaderText());
}
});
// ...(你的代码)
}
// ...(你的代码)
}
以下是我未来类的代码,但因为我在思考日期,所以里面还没有任何内容:
public class Event {
public String Name;
public String Date; // 我不知道在这里应该指定什么类型的变量,目前我指定为字符串。
public String Comment;
public String Type;
public String Notify;
Event() {
Name = "事件";
Date = "日期";
Comment = "评论";
Type = "类型";
Notify = "通知";
}
}
希望这些代码对你有所帮助。如果你有其他问题,请随时提问。
英文:
My application uses MaterialDatePicker. I want to create a class that, among other data, will store the date from the MaterialDatePicker.
I have some questions:
- What is the best way to save the date in the class if it is supposed to be changeable?
- How to get the date from the date picker material for this?
Here's my activity where I'm using the date picker material. By clicking on the checkbox, I want to save all this information, including the date, into a class object.
Here is my code of activity with MaterialDatePicker
public class NewEvent extends AppCompatActivity {
TextInputEditText etDate, etEventId;
AutoCompleteTextView etType, etNotif;
MaterialToolbar toolbarNE;
SharedPreferences sharedPreferences;
private static final String TAG = "myLogs";
static final String newEv = "newEv";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_event);
final FragmentManager fragmentManager = getSupportFragmentManager();
final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
final FirstFragment fragment1 = new FirstFragment();
etDate = findViewById(R.id.etDate);
etType = findViewById(R.id.etType);
etNotif = findViewById(R.id.etNotif);
etEventId = findViewById(R.id.etEventId);
toolbarNE = findViewById(R.id.menuNewEvent);
toolbarNE.setOnMenuItemClickListener(new MaterialToolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch(item.getItemId())
{
case R.id.applyEvent:
/*Bundle bundle = new Bundle();
bundle.putString("name", (String) etEventId.getText().toString());
fragment1.setArguments(bundle);
fragmentTransaction.replace(R.id.q3, fragment1);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();*/
Intent intent = new Intent(NewEvent.this, FirstFragment.class);
intent.putExtra("name", (String) etEventId.getText().toString());
setResult(RESULT_OK, intent);
finish();
//Toast.makeText(getApplicationContext(), materialDatePicker.getHeaderText(), Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
});
// MaterialDatePicker stuff
MaterialDatePicker.Builder builder = MaterialDatePicker.Builder.datePicker();
builder.setTitleText("Выберите дату");
final MaterialDatePicker materialDatePicker = builder.build();
etDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
materialDatePicker.show(getSupportFragmentManager(), "DATE_PICKER");
}
});
materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
@Override
public void onPositiveButtonClick(Object selection) {
etDate.setText(materialDatePicker.getHeaderText());
}
});
// Тип события
List listEvent = new ArrayList();
Collections.addAll(listEvent, "Material", "Shit");
ArrayAdapter adapterEvent = new ArrayAdapter(this,R.layout.list_item,listEvent);
etType.setAdapter(adapterEvent);
// Тип уведомления
List listNotif = new ArrayList();
//items.add("Material");
//items.add("Shit");
Collections.addAll(listNotif, "Каждый день", "Раз в 2 дня", "Раз в 3 дня", "Раз в 5 дней", "Раз в неделю", "Не напоминать");
//String [] ITEMS = new String[] {"S1", "S2", "S3"};
//ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.list_item, ITEMS);
ArrayAdapter adapterNotif = new ArrayAdapter(this,R.layout.list_item, listNotif);
etNotif.setAdapter(adapterNotif);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
Here is my code for the future class, but there is nothing in it yet, because I was thinking about the date.
public class Event {
public String Name;
public String Date; // I don't know what type of variable to specify here, so far I have specified a string.
public String Comment;
public String Type;
public String Notify;
Event()
{
Name = "Событие";
Date = "Date";
Comment = "Comment";
Type = "Type";
Notify = "Notify";
}
}
答案1
得分: 9
获取日期的方法:
MaterialDatePicker<Long> picker = builder.build();
picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
@Override public void onPositiveButtonClick(Long selection) {
// 执行某些操作...
}
});
通过这种方式,您将以毫秒为单位获得所选日期。
如果您想将其格式化为字符串,可以使用类似以下的代码:
Calendar utc = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
utc.setTimeInMillis(selection);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String formatted = format.format(utc.getTime());
英文:
To get the date use:
MaterialDatePicker<Long> picker = builder.build();
picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
@Override public void onPositiveButtonClick(Long selection) {
// Do something...
}
});
In this way you have the selected date as milliseconds.
If you want to format as a String you can use something like:
Calendar utc = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
utc.setTimeInMillis(selection);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String formatted = format.format(utc.getTime());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论