【问题标题】:Date from datepicker dialog not getting set in textview of the popup android日期选择器对话框中的日期未在弹出 android 的文本视图中设置
【发布时间】:2015-07-25 02:29:32
【问题描述】:

我的问题是 onDateSet 方法,我如何从我的 textview 字段上的 datepicker 对话框中检索选定的日期,这是一个嵌入在弹出窗口中的活动。我得到空指针异常: ((TextView) getActivity().findViewById(R.id.spinner_date)).setText("Date"+year);

DatePickerFragment.java

public class DatePickerFragment extends DialogFragment
    implements DatePickerDialog.OnDateSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the 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);

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


public void onDateSet(DatePicker view, int year, int month, int day) {
     Log.w("DatePicker", "Date = " + year);
  ((TextView) getActivity().findViewById(R.id.spinner_date)).setText("Date"+year);
}

}

在 MainActivity.java 中

     public void createVideoEvent(View arg0) {
    LayoutInflater layoutInflater
            = (LayoutInflater) getBaseContext()
            .getSystemService(LAYOUT_INFLATER_SERVICE);
    popupView = layoutInflater.inflate(R.layout.activity_event_create,          null);
    final PopupWindow popupWindow = new PopupWindow(
            popupView,
           400,
            LayoutParams.WRAP_CONTENT);
    View new_event_button = (View) findViewById(R.id.button);
    popupWindow.showAsDropDown(new_event_button);

   dateSpinner = (TextView) popupView.findViewById(R.id.spinner_date);

    View.OnTouchListener Spinner_OnTouch = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                showDatePickerDialog(v);
            }
            return true;
        }
    };
    View.OnKeyListener Spinner_OnKey = new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
                showDatePickerDialog(v);
                return true;
            } else {
                return false;
            }
        }
    };

    dateSpinner.setOnTouchListener(Spinner_OnTouch);
    dateSpinner.setOnKeyListener(Spinner_OnKey);
     SimpleDateFormat sdf = new SimpleDateFormat("cccc, MMMM dd, yyyy", Locale.US);
    dateSpinner.setText(sdf.format(myCalendar.getTime()));

}
public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getFragmentManager(), "Date");

}

【问题讨论】:

    标签: android


    【解决方案1】:

    首先您要在 DialogFragment 中创建一个接口并将数据发送到该接口。接下来,您要在活动中实现该接口的侦听器。在该侦听器中,您将设置文本。应该没有任何问题,但如果有,请告诉我,我会尽力提供帮助。

    DatePickerFragment首先要在这个类中创建一个接口,然后在活动中实现它。

    import android.app.Activity;
    import android.app.DatePickerDialog;
    import android.app.Dialog;
    import android.os.Bundle;
    import android.support.v4.app.DialogFragment;
    import android.widget.DatePicker;
    
    import java.util.Calendar;
    
    
    public class DatePickerFragment extends DialogFragment
            implements DatePickerDialog.OnDateSetListener {
    
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            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(), this, year, month, day);
        }
    
    
        public void onDateSet(DatePicker view, int year, int month, int day) {
            mCallbacks.setTextDate(year, month, day);
        }
    
        /**
         * Interface to communicate to the parent activity (MainActivity.java)
         */
        private FragmentCallbacks mCallbacks;
    
        public interface FragmentCallbacks {
            void setTextDate(int year, int month, int day);
        }
    
        @Override
        public void onAttach(Activity activity) { // Attach it to the activity
            super.onAttach(activity);
            try {
                mCallbacks = (FragmentCallbacks) activity;
            } catch (ClassCastException e) {
                throw new ClassCastException("Activity must implement Fragment One.");
            }
        }
    
        @Override
        public void onDetach() { // Remove the listener
            super.onDetach();
            mCallbacks = null;
        }
    }
    

    MainActivity 确保在扩展类之后调用 implements DatePickerFragment.FragmentCallbacks!

    import android.os.Bundle;
    import android.support.v4.app.DialogFragment;
    import android.support.v7.app.AppCompatActivity;
    import android.widget.TextView;
    
    // Make sure you implement the listener in the DatePickerFragment()
    public class MainActivity extends AppCompatActivity implements DatePickerFragment.FragmentCallbacks {
        private TextView dateSpinner;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            // Removed the touch listeners for this example to make it simple.
            // Show dialog on start for this example
            showDatePickerDialog();
    
            dateSpinner = (TextView) findViewById(R.id.spinner_date);
        }
    
        // Changed it to getSupportFragmentManager(),
        // You might need to change yours back to getFragmentManager()
        public void showDatePickerDialog() {
            DialogFragment newFragment = new DatePickerFragment();
            newFragment.show(getSupportFragmentManager(), "Date");
        }
    
        // The interface listener created in the DatePickerFragment()
        @Override
        public void setTextDate(int year, int month, int day) {
            dateSpinner.setText("Date Year: " + year);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 2015-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-23
      • 1970-01-01
      相关资源
      最近更新 更多