2013-03-08 204 views
4

我使用的DatePicker我的應用程序。我想,我已經選擇(在DatePicker)的日期,但它沒有返回選定的日期。它總是返回當前日期。我如何才能從DatePicker獲得所選日期?如何從android中的日期選擇器中獲取日期?

任何幫助將不勝感激。

day = pickDate.getDayOfMonth(); 
    month = pickDate.getMonth() + 1; 
    year = pickDate.getYear(); 
    readAndChoose = (Button) findViewById(R.id.btnReadAndChoose); 
    readAndChoose.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

      Context context = getApplicationContext(); 
      CharSequence text = "" + day + "-" + "" + month + "-" + "" 
        + year; 
      int duration = Toast.LENGTH_SHORT; 
      Toast toast = Toast.makeText(context, text, duration); 
      toast.show(); 

      /* 
      * Intent i = new Intent(BirthActivity.this, 
      * CompassActivity.class); startActivity(i); 
      */ 

     } 

    }); 

我不想創建一個DatePicker對話,但我想透過Android提供的DatePicker。

+0

更新與您正在使用的代碼的OP。 – 2013-03-08 09:53:47

+0

您需要爲日期選擇器設置偵聽器並獲取事件觸發的日期,onDateSetlistener – Pragnani 2013-03-08 10:01:18

+0

如何設置日期選擇器的偵聽器。 – user2147892 2013-03-08 10:26:47

回答

1

我覺得這是你需要

// the call back received when the user "sets" the date in the dialog 
private DatePickerDialog.OnDateSetListener mDateSetListener = 
    new DatePickerDialog.OnDateSetListener() { 
     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
      mYear = year; 
      mMonth = monthOfYear; 
      mDay = dayOfMonth; 

     } 
}; 
2
public class pickerdate extends Activity { 
/** Called when the activity is first created. */ 
private TextView mDateDisplay; 
private Button mPickDate; 
private int mYear; 
private int mMonth; 
private int mDay; 

static final int DATE_DIALOG_ID = 0; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 


    mDateDisplay = (TextView) findViewById(R.id.dateDisplay); 
    mPickDate = (Button) findViewById(R.id.pickDate); 


    mPickDate.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      showDialog(DATE_DIALOG_ID); 
     } 
    }); 


    final Calendar c = Calendar.getInstance(); 
    mYear = c.get(Calendar.YEAR); 
    mMonth = c.get(Calendar.MONTH); 
    mDay = c.get(Calendar.DAY_OF_MONTH); 

    updateDisplay(); 
} 
private void updateDisplay() { 
    mDateDisplay.setText(
     new StringBuilder() 
       // Month is 0 based so add 1 
       .append(mMonth + 1).append("-") 
       .append(mDay).append("-") 
       .append(mYear).append(" ")); 
} 
private DatePickerDialog.OnDateSetListener mDateSetListener = 
    new DatePickerDialog.OnDateSetListener() { 

     public void onDateSet(DatePicker view, int year, 
           int monthOfYear, int dayOfMonth) { 
      mYear = year; 
      mMonth = monthOfYear; 
      mDay = dayOfMonth; 
      updateDisplay(); 
     } 
    }; 
    @Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
     case DATE_DIALOG_ID: 
      return new DatePickerDialog(this, 
         mDateSetListener, 
         mYear, mMonth, mDay); 
     } 
     return null; 
    } 
} 

主要佈局文件

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 
<TextView android:id="@+id/dateDisplay" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text=""/> 
<Button android:id="@+id/pickDate" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Date Picker"/> 
    </LinearLayout> 

上面的代碼工作是什麼。我希望這正是你的期望。

+0

我不想使用單獨的對話框。但我想用android提供的日期選擇器。 – user2147892 2013-03-08 10:43:54

+0

注意:**不再使用。你應該考慮使用'DatePickerFragment'而不僅僅是對話框** – Raghunandan 2014-04-23 10:00:46

3
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) { 
      year = selectedYear; 
      month = selectedMonth; 
      day = selectedDay; 

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

      // set selected date into datepicker also 
      dpResult.init(year, month, day, null); 

     } 
    }; 

參考Thsi link..May是這將有助於you.:- http://www.mkyong.com/android/android-date-picker-example/

0

DJ的答案會工作。如果你想使用日曆變量來存儲日期然後,

 public void onDateSet(DatePicker view, int year, int monthOfYear, 
      int dayOfMonth) { 
     setDate.set(year, monthOfYear, dayOfMonth);   

希望這有助於!

0

也有一個偉大的教程here其中也有源代碼下載。

有一點要注意,編譯源代碼時,你需要做SelectDateFragment類「靜態」的javapapers代碼,以及使populateSetDate()方法靜態的,而且我也感動這段代碼:

mEdit = (EditText)findViewById(R.id.editText1);

到onCreate()方法。之後,你可以編譯代碼。

MainActivity應該是這樣到底編輯後:

package com.javapapers.andoiddatepicker; 
import java.util.Calendar; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.os.Bundle; 
import android.support.v4.app.DialogFragment; 
import android.support.v4.app.FragmentActivity; 
import android.view.View; 
import android.widget.DatePicker; 
import android.widget.EditText; 

public class MainActivity extends FragmentActivity { 

private static EditText mEdit; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mEdit = (EditText) findViewById(R.id.editText1); 
} 

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

public static void populateSetDate(int year, int month, int day) { 
    mEdit.setText(month + "/" + day + "/" + year); 
} 

public static class SelectDateFragment 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) { 
     populateSetDate(year, month+1, day); 
    } 
} 

}

2

有2種方法可以這樣做:使用DatePicker傳球init()方法

  1. OnDateChangedListener

    datePicker.init(
         datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth(), 
         new DatePicker.OnDateChangedListener() { 
          @Override 
          public void onDateChanged(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) { 
           selectedYear = year; 
           selectedMonth = monthOfYear; 
           selectedDayOfMonth = dayOfMonth; 
           Log.d(LOG_TAG, "selectedDate = " + selectedYear + "-" + selectedMonth + "-" + selectedDayOfMonth); 
           //TODO save or use selected value here 
          }//end onDateChangedListener 
    
    }); 
    
  2. 有一個按鈕點擊或壓倒一切的獲取當前的選擇日期時,某個動作是要採取或當用戶離開屏幕。

    @Override 
    public void onPause() { 
        int selectedYear = datePicker.getYear(); 
        int selectedMonth = datePicker.getMonth(); 
        int selectedDay = datePicker.getDayOfMonth(); 
        //TODO save or use selected value here 
    } 
    
相關問題