2013-04-29 98 views
0

好吧,所以我有這個應用程序我正在...它有一個左側菜單滑出使用ABS和滑動菜單庫。這是我選擇菜單項後啓動的片段。這個片段假設有一個文本字段來顯示你選擇的日期和一個按鈕來選擇一個日期。任何想法是我做錯了SelectDateFragment錯誤?Android的DatePicker按鈕不工作

這裏的XML佈局:

<EditText 
     android:id="@+id/dateselected" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="20dp" 
     android:layout_marginTop="56dp" 
     android:ems="10" 
     android:inputType="date" > 

     <requestFocus /> 
    </EditText> 

    <Button 
     android:id="@+id/pickdate" 
       android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/dateselected" 
     android:layout_toRightOf="@+id/dateselected" 
     android:contentDescription="@string/selectdate" 
     android:cropToPadding="true" 
     android:onClick="selectDate" 
     android:src="@drawable/ic_datepicker" /> 

</RelativeLayout> 

這裏的java類:

import java.util.Calendar; 
import android.annotation.SuppressLint; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.DialogFragment; 
import android.widget.EditText; 


public class DatePicker extends Fragment { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstaceState){ 
     return inflater.inflate(R.layout.fragment_charting, container, false); 
    } 

    EditText mEdit; 
    @Override 
    public void onStart() { 
     super.onStart(); 
    } 


    public void selectDate(View view) { 
     DialogFragment newFragment = new SelectDateFragment(); 
     newFragment.show(getFragmentManager(), "DatePicker"); 
    } 

    public void populateSetDate(int year, int month, int day) { 
     mEdit = (EditText)getView().findViewById(R.id.dateselected); 
     mEdit.setText(month+"/"+day+"/"+year); 
    } 

    public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { 

     @Override 
     public Dialog onCreateDialog(Bundle savedInstanceState) { 
      final Calendar calendar = Calendar.getInstance(); 
      int yy = calendar.get(Calendar.YEAR); 
      int mm = calendar.get(Calendar.MONTH); 
      int dd = calendar.get(Calendar.DAY_OF_MONTH); 
      return new DatePickerDialog(getActivity(), this, yy, mm, dd); 
     } 

     @Override 
     public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
      // TODO Auto-generated method stub 
      populateSetDate(year, monthOfYear, dayOfMonth); 

     } 
    } 

} 

編輯: 我試圖按照http://javapapers.com/android/android-datepicker/

我得到的錯誤是片段內部類應該是靜態的。我對這個話題不夠了解,從更多的閱讀中我仍然不明白,爲什麼他的作品和我的作品都沒有。

+0

它到底是什麼示數? – bofredo 2013-04-29 14:42:55

+0

public class SelectDateFragment – jcaruso 2013-04-29 16:51:41

+0

你可以將它設置爲靜態,如果不打算在晚些時候改變它。靜態和最終,檢查一些java源。你會更頻繁地找到這個! – bofredo 2013-04-29 17:02:04

回答

1

試試這個

shipdate.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      showDialog(999); 

     } 

    }); 




@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case 999: 
     // set date picker as current date 
     return new DatePickerDialog(this, datePickerListener, year, month, 
       day); 
    } 
    return null; 
} 



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

    // when dialog box is closed, below method will be called. 
    public void onDateSet(DatePicker view, int selectedYear, 
      int selectedMonth, int selectedDay) { 
     final Calendar cf = Calendar.getInstance(); 
     year = cf.get(Calendar.YEAR); 
     month = cf.get(Calendar.MONTH); 
     day = cf.get(Calendar.DAY_OF_MONTH); 



      // set selected date into textview 
      shipdate.setText(new StringBuilder().append(month + 1) 
        .append("-").append(day).append("-").append(year) 
        .append(" ")); 
     } 


}; 
+0

我是新來的機器人,仍然閱讀很多書籍。所以你將不得不說明如何使用這段代碼,因爲當我將它粘貼到我的片段中並導入它的所有內容時,它仍然會出錯。 – jcaruso 2013-04-29 14:49:24