我想显示日期选择器弹出窗口。我找到了一些例子,但我没有正确地理解它。我有一个编辑文本,然后单击编辑文本,将弹出日期选择器对话框,设置日期后,该日期应以dd / mm / yyyy格式显示在片段中的编辑文本中。请为我提供示例代码或良好的链接。
答案 0 :(得分:2)
解决方案:
让您的EditText clickable and focusable false
像
<EditText
android:id="@+id/etDOB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:hint="Select" />
然后在您的Fragment Class中,将代码放在下面,
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout_xxx, container, false);
EditText etDOB=view.findViewById(R.id.etDOB);
etDOB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openDatePickerDialog(v);
}
});
}
public void openDatePickerDialog(final View v) {
// Get Current Date
DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
(view, year, monthOfYear, dayOfMonth) -> {
String selectedDate = dayOfMonth + "/" + (monthOfYear + 1) + "/" + year;
switch (v.getId()) {
case R.id.etDOB:
((EditText)v).setText(selectedDate);
break;
}
}, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH));
datePickerDialog.getDatePicker().setMaxDate(cal.getTimeInMillis());
datePickerDialog.show();
}
答案 1 :(得分:1)
在XML文件中尝试一下:
<EditText
android:id="@+id/Birthday"
custom:font="@string/font_avenir_book"
android:clickable="true"
android:editable="false"
android:hint="@string/birthday"/>
现在在Java文件中:
Calendar myCalendar = Calendar.getInstance();
EditText edittext= (EditText) findViewById(R.id.Birthday);
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
edittext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(classname.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"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
edittext.setText(sdf.format(myCalendar.getTime()));
}
在EditText的xml文件中添加android:focusable="false"
,以实现一次触摸。
答案 2 :(得分:0)
第1步。禁用您的编辑文本
'enabled=false'
第2步。在“编辑文本”上设置onClickListener
第3步。在您的EditText上单击,调用该函数以打开“日期选择器”对话框。
和onDateSelected,重写的函数将返回日期,您可以从那里提取日期,月份和年份,然后在“编辑”文本中将该文本设置为字符串。
让我知道您是否需要代码帮助。
答案 3 :(得分:0)
我已经找到了一种简便的方法。这是一次投资工作
将以下类复制到您的项目中...
public class DatePickerHelper {
/********************************************DatePickerRelatedWork*****************************/
public static void showDatePicker(Activity activity, OnDateSelectedListener listener, boolean shouldUsePreviousDate) {
Calendar currentDate = Calendar.getInstance();
int currentYear = currentDate.get(Calendar.YEAR);
int currentMonth = currentDate.get(Calendar.MONTH);
int currentDay = currentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePicker = new DatePickerDialog(activity, new MyDateSetListener(listener), currentYear, currentMonth, currentDay);
datePicker.setTitle("Select date");
if (shouldUsePreviousDate){
datePicker.getDatePicker().setMinDate(-1893475799000L);
}else {
datePicker.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
}
datePicker.show();
}
public static class MyDateSetListener implements DatePickerDialog.OnDateSetListener {
private OnDateSelectedListener listener;
public MyDateSetListener(OnDateSelectedListener listener) {
this.listener = listener;
}
@Override
public void onDateSet(DatePicker datepicker, int selectedYear, int selectedMonth, int selectedDay) {
String selectedDateString = null;
selectedMonth = selectedMonth + 1;
if (selectedDay >= 0 && selectedDay < 10 && selectedMonth >= 0 && selectedMonth < 10) {
selectedDateString = "0" + selectedDay + "-" + "0" + selectedMonth + "-" + selectedYear;
} else if (selectedDay >= 0 && selectedDay < 10) {
selectedDateString = "0" + selectedDay + "-" + selectedMonth + "-" + selectedYear;
} else if (selectedMonth >= 0 && selectedMonth < 10) {
selectedDateString = selectedDay + "-" + "0" + selectedMonth + "-" + selectedYear;
} else {
selectedDateString = selectedDay + "-" + selectedMonth + "-" + selectedYear;
}
listener.onDateSelected(selectedDateString);
}
}
public interface OnDateSelectedListener {
void onDateSelected(String selectedDateString);
}
}
然后将以下方法复制到您的片段中...
private void showDatePickerDialog(){
DatePickerHelper.showDatePicker(AddCashbookActivity.this, new DateTimePickerWork.OnDateSelectedListener() {
@Override
public void onDateSelected(String selectedDateString) {
editText.setText(selectedDateString);
}
},true);
}
然后从您的EditText侦听器(都是
OnFocusChangeListener
和
OnClickListener
)
下面是一个示例...
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus){
showDatePickerDialog();
}
}
});
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showDatePickerDialog();
}
});
更改
editText
可变为您的EditText变量名称。
您完成了!
了解代码
上面的类可以在您的项目中多次使用,您无需考虑Helper类的内部工作。困难的部分已经由该类完成,并且只有在选择日期时您才会获得回调。
showDatePicker
方法需要3个参数。
如果您需要更改日期格式,则可以更改方法
onDateSet
您可以将10-05-2018更改为10/05/2018或其他任何内容。只需阅读该方法的正文,您就会了解要更改的内容。
希望这会对您和其他许多人有所帮助。
编辑
在EditText的xml文件中添加android:focusable="false"
,以便进行一次触摸,然后您可以避免在Java代码中使用setOnFocusChangeListener
。
答案 4 :(得分:0)
Edittext edittext= findViewById(R.id.edittext);
private void getDate(){
edtitext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar mcurrentDate = Calendar.getInstance();
mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
mMonth = mcurrentDate.get(Calendar.MONTH);
mYear = mcurrentDate.get(Calendar.YEAR);
DatePickerDialog datePickerDialog = new DatePickerDialog(AddDetails.this, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
month = month + 1;
date = dayOfMonth + ":" + month + ":" + year;
edittext.setText(date);
}
}, mYear, mMonth, mDay);
datePickerDialog.show();
}
});
}
答案 5 :(得分:0)
您可以将Edittext
与custom layout
一起使用,而不使用Textview
。在点击该布局后,您可以打开日期选择器对话框,并从datepicker
中选择日期并用该日期更新Textview
用于布局的XML
<RelativeLayout
android:id="@+id/layout_custom"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/background_rectangle">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv_birth_date"
android:textColor="@color/black"
android:text="@string/date_format"
android:layout_centerInParent="true"
/>
</RelativeLayout>
在drawable
文件夹中通过名称background_rectangle
创建一个新文件,并将以下代码粘贴到其中
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<!-- This is the stroke you want to define -->
<stroke android:width="1dp"
android:color="@color/black"/>
<!-- Optional, round your corners -->
<corners android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:bottomRightRadius="5dp"
android:topRightRadius="5dp" />
<!-- Optional, fill the rest of your background with a color or gradient, use transparent if you only want the border to be displayed-->
<gradient android:startColor="@android:color/transparent"
android:endColor="@android:color/transparent"
android:angle="90"/>
现在在app
级gradle file
上添加依赖项
implementation 'com.wdullaer:materialdatetimepicker:2.3.0'
现在内部活动实现了datapickerdialog.ondateselected
方法
class ABCActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener,OnClickListener{
DatePickerDialog datePickerDialog;
TextView textViewDate;
RelativeLayout customLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_abc);
// findviewsby id
customLayout.setOnclickListener(this);
}
public void onClick(View v) {
Calendar maxDate = Calendar.getInstance();
maxDate.add(Calendar.DATE, 0);
calendar = Calendar.getInstance();
datePickerDialog = DatePickerDialog.newInstance(
CalculatorAgeActivity.this,
calendar.get(Calendar.YEAR), // Initial year selection
calendar.get(Calendar.MONTH), // Initial month selection
calendar.get(Calendar.DAY_OF_MONTH) // Inital day selection
);
datePickerDialog.setThemeDark(false);
datePickerDialog.setAccentColor(Color.parseColor("#58a5f0"));
datePickerDialog.setTitle("Select Date");
datePickerDialog.show(getFragmentManager(), "DatePicker Dialog");
}
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
textViewDate.setText(Integer.toString(dayOfMonth) + "/" + Integer.toString(monthOfYear + 1) + "/" +
Integer.toString(year));
}}