2014-11-15 26 views
0

我正在構建一個事件託管的Android應用程序。 在託管一個事件時,用戶填寫一些表單。如何在圖像按鈕上設置datapicker單擊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); 
      } 

     }; 

} 

回答

1

只需直接調用dailog datapicker片段在圖像按鈕。

在這裏我有你的問題的代碼。

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 
        } 
       } 
相關問題