DatepickerDialog 不在 Textview 中添加日期
Posted
技术标签:
【中文标题】DatepickerDialog 不在 Textview 中添加日期【英文标题】:DatepickerDialog doesnt add Date in Textview 【发布时间】:2020-08-11 09:26:08 【问题描述】:我有一个问题。 我在 android Studio 中创建了一个 DatePickerDialog,但是当显示 Datepickerdialog 并且我想添加一个日期时,它不起作用。当我按下按钮并因此更改日期时,我想看到 DialogPicker,但文本视图中没有任何内容。我的代码在这里。如果有人告诉我如何按月、日、年的顺序制作日期也很好
片段类:
公共类 Fragment 扩展 Fragment 实现 DatePickerDialog.OnDateSetListener
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
calendar = Calendar.getInstance();
Button button = (Button) getView().findViewById(R.id.change);
button.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
DialogFragment datepicker = new DatePickerFragment();
datepicker.show(getFragmentManager(), "date picker");
);
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth)
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
String currentime = DateFormat.getDateInstance(DateFormat.FULL).format(calendar.getTime());
TextView textView = getView().findViewById(R.id.date);
textView.setText(currentime);
DatePicker 类: 公共类 DatePickerFragment 扩展 DialogFragment
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState)
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(),(DatePickerDialog.OnDateSetListener) getTargetFragment(),
year, month, day);
【问题讨论】:
【参考方案1】:fragment_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_
android:layout_
orientation="Vertical"
tools:context=".MainFragment">
<TextView
android:layout_
android:layout_
android:hint="Date"
android:id="@+id/Date"
android:layout_marginTop="50dp"
android:layout_centerHorizontal="true"
android:textSize="30sp"/>
<Button
android:id="@+id/tvDate"
android:layout_
android:layout_
android:hint="Select Date"
/>
</LinearLayout>
MainFragment.java
public class MainFragment extends Fragment
private static final String TAG = "MainFragment";
private TextView date;
private Button mDisplayDate;
private DatePickerDialog.OnDateSetListener mDateSetListener;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment, container, false);
mDisplayDate=view.findViewById(R.id.tvDate);
date=view.findViewById(R.id.Date);
mDisplayDate.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dialog = new DatePickerDialog(
MainActivity.this,
android.R.style.Theme_Holo_Light_Dialog_MinWidth,
mDateSetListener,
year,month,day);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
);
mDateSetListener = new DatePickerDialog.OnDateSetListener()
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day)
month = month + 1;
Log.d(TAG, "onDateSet: mm/dd/yyy: " + month + "/" + day + "/" + year);
String date = month + "/" + day + "/" + year;
date.setText(date);
;
return view;
【讨论】:
我如何使用片段而不是主要活动来做到这一点? 请检查上面的代码。我更新了那个程序。以上是关于DatepickerDialog 不在 Textview 中添加日期的主要内容,如果未能解决你的问题,请参考以下文章
Android DatePickerDialog和TimePickerDialog显示样式
DatePickerDialog日期对话框以及回调函数的用法
android笔记:DatePickerDialog日期设置对话框