作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序使用 MaterialDatePicker。我想创建一个类,除其他数据外,该类将存储 MaterialDatePicker 中的日期。我有一些问题:
这是我使用 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/
我是一名优秀的程序员,十分优秀!