0

我正在构建一个托管事件的 android 应用程序。在举办活动时,用户填写一些表格。

现在为了填充数据,我正在使用数据选择器的对话框。我可以在活动中执行此操作,但是当我在 Fragmented 中实现它时,此代码不起作用。

当用户单击图像按钮时,我需要显示一个数据选择器对话框。这是代码:

   package tabsswipe;


    public class FragmentOne extends Fragment implements OnClickListener{

    private ImageButton ib;
    private Calendar cal;
    private int day;
    private int month;
    private int year;
    private EditText et;

    private static final String TAG = FragmentOne.class.getSimpleName();

    Calendar c = Calendar.getInstance();
    int startYear = c.get(Calendar.YEAR);
    int startMonth = c.get(Calendar.MONTH);
    int startDay = c.get(Calendar.DAY_OF_MONTH);
    Spinner spnr;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {


View rootView = inflater.inflate(R.layout.fragment_play, container, false);


    ImageButton btnhost = (ImageButton) getActivity().findViewById(R.id.hostbutton);
    btnhost.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            viewCategory();

        }
    });


return rootView;
}

 private void viewCategory() {

    AlertDialog.Builder viewDialog = new AlertDialog.Builder(getActivity());

    viewDialog.setTitle("Event");

    LayoutInflater li = (LayoutInflater) 

 getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View dialogView = li.inflate(R.layout.customealertdialogbox, null);
    viewDialog.setView(dialogView);

    viewDialog.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {




                }
            });

    viewDialog.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            });


    ib = (ImageButton) dialogView.findViewById(R.id.imageButton1);
    cal = Calendar.getInstance();
    day = cal.get(Calendar.DAY_OF_MONTH);
    month = cal.get(Calendar.MONTH);
    year = cal.get(Calendar.YEAR);
    et = (EditText) dialogView.findViewById(R.id.editText);
    ib.setOnClickListener(this);
    viewDialog.show();

}
        @Override
        public void onClick(View v) {
            showDialog(0);                  
        }
        private DatePickerDialog showDialog(int i) {
            // TODO Auto-generated method stub


            return new DatePickerDialog(getActivity(), datePickerListener, year, month, day);
        }

        private DatePickerDialog.OnDateSetListener datePickerListener = new 
        DatePickerDialog.OnDateSetListener() {


            public void onDateSet(DatePicker view, int selectedYear,
                    int selectedMonth, int selectedDay) {

                et.setText(selectedDay + " / " + (selectedMonth + 1) + " / "
                        + selectedYear);
            }

        };

 }
4

1 回答 1

1

只需直接在图像按钮中调用 dailog 数据选择器片段即可。

根据您的问题,我在这里有代码。

ib.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                DialogFragment newFragment = new DatePickerFragment();
                newFragment.show(getFragmentManager(), "datePicker"); 
            }
        });



 public class DatePickerFragment extends DialogFragment implements
                DatePickerDialog.OnDateSetListener {

                @Override
                public Dialog onCreateDialog(Bundle savedInstanceSateate) {

                    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) {
                        // Do something with the date chosen
                    }
                }
于 2014-11-15T04:42:47.850 回答