gpt4 book ai didi

android - Edittext 无法从日期选择器中获取用户确定的日期

转载 作者:行者123 更新时间:2023-11-30 01:40:33 24 4
gpt4 key购买 nike

我想在单击图像按钮时弹出一个日期选择器对话框,然后用户选择的日期将显示在编辑文本字段中。我按照Android开发人员的指导通过DialogFragment使用,成功弹出了日期选择器对话框。用户可以在对话框中选择日期,但是当用户按下设置按钮时它会崩溃。

事实上,我怀疑这是 onDateSet 中 LayoutInflater 中的 getContext() 的问题,因为有评论说“调用所需的 API 级别 23(当前 API:12)”,但我无法弄清楚如何修复它。

这是Android开发者推荐调用getContext()来获取active Context的,不知道为什么会出现这个问题。

我尝试使用@TargetApi,但它说这仅用于方法。

谁能告诉我我的代码哪里有问题?

合乎逻辑的陈述:java.lang.NoSuchMethodError:com.test.testapplication.DatePickerFragment.getContext

日期选择器 fragment

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {


final Calendar calendar = Calendar.getInstance();
int YEAR;
int MONTH;
int DAY;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

YEAR = calendar.get(Calendar.YEAR);
MONTH = calendar.get(Calendar.MONTH);
DAY = calendar.get(Calendar.DAY_OF_MONTH);

return new DatePickerDialog(getActivity(), this, YEAR, MONTH, DAY);
}

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

YEAR = year;
MONTH = monthOfYear;
DAY = dayOfMonth;

String dateFormat = "MM/dd/yy";
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.CHINESE);

View fragment = LayoutInflater.from(getContext()).inflate(R.layout.activity_datepickerdialogfragment, null);
EditText editText = (EditText) fragment.findViewById(R.id.edittext);
editText.setText(sdf.format(calendar.getTime()));
}
}

主要 Activity

public class DPDFActivity extends FragmentActivity {

EditText editText;
ImageButton btn;
DialogFragment datePickerFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datepickerdialogfragment);

editText = (EditText) findViewById(R.id.edittext);
btn = (ImageButton) findViewById(R.id.btn);

btn.setOnClickListener(new showDateDialog());
}

public class showDateDialog implements View.OnClickListener {
@Override
public void onClick(View v) {
datePickerFragment = new DatePickerFragment();
datePickerFragment.show(getFragmentManager(), "DatePicker");
}
}
}

最佳答案

在你的 Activity 中创建日期选择器类作为静态的,例如我在我的 fragment 中做了类似的事情。

public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
int currentYear;

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
currentYear = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, currentYear, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
if(currentYear-year >=15) {
CharSequence strDate = null;
Calendar chosenDate = Calendar.getInstance();
chosenDate.setTimeInMillis(0);
chosenDate.set(year, month, day);
Date date = chosenDate.getTime();
SimpleDateFormat dateFormatter = new SimpleDateFormat(
"yyyy-MM-dd");
strDate = dateFormatter.format(date);

btnDOB.setText(strDate);
}
else
{
Toast.makeText(view.getContext(),"Selected Year should be mininmum 15 year ago from current year. Please try again.",Toast.LENGTH_LONG).show();
}
}
}

在任何 methon(类级别)之外的 Activity 中定义 edittext

private static EditText meditText;

仅在 Activity oncreate() 中使用 findById() 方法,稍后可以更新。

关于android - Edittext 无法从日期选择器中获取用户确定的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34566904/

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