gpt4 book ai didi

java - 如何从 MaterialDatePicker 获取日期?

转载 作者:行者123 更新时间:2023-12-04 01:15:41 25 4
gpt4 key购买 nike

我的应用程序使用 MaterialDatePicker。我想创建一个类,除其他数据外,该类将存储 MaterialDatePicker 中的日期。我有一些问题:

  1. 如果日期应该是可更改的,那么在类里面保存日期的最佳方法是什么?
  2. 如何从日期选择器 Material 中获取日期? 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.

这是我使用 MaterialDatePicker 的 Activity 代码

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);
}

这是我 future 类(class)的代码,但其中还没有任何内容,因为我正在考虑日期。

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";
}

}

最佳答案

获取日期使用:

MaterialDatePicker<Long> picker = builder.build();

picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
@Override public void onPositiveButtonClick(Long selection) {
// Do something...
}
});

这样,您选择的日期为毫秒。
如果你想格式化为一个字符串,你可以使用类似的东西:

Calendar utc = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
utc.setTimeInMillis(selection);
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String formatted = format.format(utc.getTime());

关于java - 如何从 MaterialDatePicker 获取日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63447878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com