我想显示日期选择器弹出窗口。我找到了一些例子,但我没有得到正确的。我有一个edittext,我希望当我点击edittext时,datepicker对话框应该弹出,设置日期后,日期应该显示在edittext在dd/mm/yyyy格式。请为我提供示例代码或良好的链接。
当前回答
在XML文件中试试这个:
<EditText
android:id="@+id/Birthday"
custom:font="@string/font_avenir_book"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="@string/birthday"/>
在Java文件中是这样的:
public class MainActivity extends AppCompatActivity {
final Calendar myCalendar= Calendar.getInstance();
EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText) findViewById(R.id.BirthDate);
DatePickerDialog.OnDateSetListener date =new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH,month);
myCalendar.set(Calendar.DAY_OF_MONTH,day);
updateLabel();
}
};
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DatePickerDialog(MainActivity.this,date,myCalendar.get(Calendar.YEAR),myCalendar.get(Calendar.MONTH),myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
private void updateLabel(){
String myFormat="MM/dd/yy";
SimpleDateFormat dateFormat=new SimpleDateFormat(myFormat, Locale.US);
editText.setText(dateFormat.format(myCalendar.getTime()));
}
}
在EditText的xml文件中添加android:focusable="false"以允许单个触摸。
其他回答
import android.app.DatePickerDialog;
import android.content.Context;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class DatePickerHelper {
private final Calendar calendar = Calendar.getInstance();
private final String dateFormat = "MM/dd/yy";
private TextView textView = null;
public DatePickerHelper(final Context context, TextView textView) {
this.textView = textView;
// Setup on click listener if the TextView is not null
if (textView != null) {
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getDatePickerDialog(context).show();
}
});
}
}
/**
* Return a new date picker listener tied to the specified TextView field
* @return
*/
private DatePickerDialog.OnDateSetListener getOnDateSetListener() {
return new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, monthOfYear);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.US);
textView.setText(sdf.format(calendar.getTime()));
}
};
}
/**
* Return new DatePickerDialog for field
* @param context
* @return
*/
private DatePickerDialog getDatePickerDialog(Context context) {
return new DatePickerDialog(context, getOnDateSetListener(), calendar
.get(Calendar.YEAR), calendar.get(Calendar.MONTH),
calendar.get(Calendar.DAY_OF_MONTH));
}
}
用法:
DatePickerHelper assessmentDueDateHelper = new DatePickerHelper(AssessmentsDetailActivity.this,
(TextView) findViewById(R.id.assessmentDueDateEditText));
selectDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatePickerDialog mdiDialog =new DatePickerDialog(mContext,new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Toast.makeText(getApplicationContext(),year+ " "+monthOfYear+" "+dayOfMonth,Toast.LENGTH_LONG).show();
}
}, year, month, date);
mdiDialog.show();
}
});
试试这个:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
DataPickerListener listener;
public DatePickerFragment(DataPickerListener l) {
listener = l;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the date picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(),
AlertDialog.THEME_HOLO_LIGHT, this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
month = month + 1;
String stringOfDate = day + "-" + month + "-" + year;
listener.setDate(stringOfDate);
Util.Log(stringOfDate);
}
@Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
super.onCancel(dialog);
listener.cancel();
}
public static interface DataPickerListener {
void setDate(String date);
void cancel();
}
}
// use this function to open datePicker popup
DialogFragment newFragment;
public void pickDate() {
if (newFragment == null)
newFragment = new DatePickerFragment(new PickDateListener());
newFragment.show(getFragmentManager(), "Date Picker");
}
class PickDateListener implements DataPickerListener {
@Override
public void setDate(String date) {
createSpiner(date);
onetouch = false;
}
@Override
public void cancel() {
onetouch = false;
}
}
这在2020年1月对我有效
XML部分:
<EditText
android:id="@+id/etDOB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:editable="false"
android:ems="10"
android:hint="Enter Your Date of Birth" />
Java的部分:
final Calendar myCalendar = Calendar.getInstance();
userDOBView = (EditText)findViewById(R.id.etDOB);
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
userDOBView.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
if(event.getAction() == MotionEvent.ACTION_DOWN){
new DatePickerDialog("YourClassName".this, date,
myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
return true;
}
});
private void updateLabel() {
String myFormat = "MM/dd/yyyy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
// edittext.setText(sdf.format(myCalendar.getTime()));
userDOBView.setText(sdf.format(myCalendar.getTime()));
}
用这个简单的技巧来做吧:
步骤1:创建一个片段对话框
public class DatePickerFragmentDialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day);
}
}
第二步:在必要的活动中遵循这一点
该活动必须实现:DatePickerDialog。OnDateSetListener 在按钮上设置onClickListener: DialogFragment datePicker = new DatePickerFragmentDialog(); datePicker.show(getSupportFragmentManager(), "自定义日期选择器");
步骤3:覆盖OnDateSetListener
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, year);
calendar.set(Calendar.MONTH, month);
calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
@SuppressLint("SimpleDateFormat") DateFormat dateFormat = new
SimpleDateFormat("MM/dd/yyyy");
String currentDateString = dateFormat.format(calendar.getTime());
tvPaymentDate.setText(currentDateString);
}
因此,我们可以使用任何格式的日期:)
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在Android中将字符串转换为Uri
- 如何在NestedScrollView内使用RecyclerView ?
- 移动到另一个EditText时,软键盘下一步点击Android
- Android应用中的GridView VS GridLayout
- Activity和FragmentActivity的区别
- 右对齐文本在android TextView
- 权限拒绝:start前台需要android.permission.FOREGROUND_SERVICE
- 如何更改android操作栏的标题和图标
- Android Split字符串
- 让一个链接在安卓浏览器启动我的应用程序?
- 如何在Android工作室的外部库中添加一个jar ?
- GridLayout(不是GridView)如何均匀地拉伸所有子元素
- 如何让一个片段删除自己,即它的等效完成()?