Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/github/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在android中设置图像按钮点击片段上的数据采集器?_Android_Android Fragments_Datapicker - Fatal编程技术网

如何在android中设置图像按钮点击片段上的数据采集器?

如何在android中设置图像按钮点击片段上的数据采集器?,android,android-fragments,datapicker,Android,Android Fragments,Datapicker,我正在构建一个android应用程序,其中有一个活动。 在主持活动时,用户填写一些表格 现在我用数据采集器的对话框来填充数据。 我可以在活动中执行此操作,但当我在片段中实现它时,此代码不起作用 我需要有当用户点击一个图像按钮,一个数据选择器对话框应该显示。 以下是代码: package tabsswipe; public class FragmentOne extends Fragment implements OnClickListener{ private Ima

我正在构建一个android应用程序,其中有一个活动。 在主持活动时,用户填写一些表格

现在我用数据采集器的对话框来填充数据。 我可以在活动中执行此操作,但当我在片段中实现它时,此代码不起作用

我需要有当用户点击一个图像按钮,一个数据选择器对话框应该显示。 以下是代码:

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

        };

 }

只需直接在图像按钮中调用dailog datapicker fragment

这里我有你的问题代码

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
                    }
                }