【问题标题】:Edittext not possible to get the date settled by user from date pickerEdittext 无法从日期选择器中获取用户确定的日期
【发布时间】:2016-01-02 14:23:27
【问题描述】:

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

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

这是Android开发者推荐调用getContext()获取活动上下文,不知道为什么会出现问题。

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

谁能告诉我代码中的问题出在哪里?

逻辑上说: java.lang.NoSuchMethodError: com.test.testapplication.DatePickerFragment.getContext

日期选择器片段

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()));
}
}

主活动

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");
    }
}
}

【问题讨论】:

    标签: android


    【解决方案1】:

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

    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();
            }
        }
    }
    

    在任何方法之外的活动中定义编辑文本(类级别)

    private static EditText meditText;
    

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

    【讨论】:

    • 您的建议很有效,使其更易于理解,并且已经在我的程序中实施。只有一件事我担心我们是否可以按照 Android 开发人员的建议去做。
    【解决方案2】:

    而不是尝试调用 getContext()。您应该可以在这里使用 getActivity()。

    View fragment = LayoutInflater.from(getActivity()).inflate(R.layout.activity_datepickerdialogfragment, null);
    

    【讨论】:

    • getActivity() 可以在没有崩溃的情况下使用,但日期设置后的编辑文本上不会显示日期。你知道出了什么问题吗?顺便问一下,你知道为什么android开发者建议在getContext()不起作用的情况下使用它吗?
    • 这里,editText.setText(sdf.format(calendar.getTime())); calendar.getTime() 返回一个 Date 对象而不是字符串。尝试使用editText.setText(sdf.format(calendar.getTime().getTime())),我知道它很难看。
    • 也不知道为什么android开发者建议使用getContext()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多