2014-01-29 62 views
0

如上所述,我試圖找出如何傳回用戶選擇的日期。我已經計算出如何使用onDateSet方法獲取選定的日期,但我不知道如何將其反饋回父片段,然後將EditText文本設置爲所選日期。從DatePickerDialog獲取數據到一個片段

package com.example.androidvehicle; 

import java.util.Calendar; 

import android.app.Activity; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.app.DialogFragment; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.EditText; 

public class fragmentServicing extends Fragment { 


    callBack mCallBack; 

    Button mot; 
    Button servicing; 
    Button tax; 
    EditText txtService; 

    final int Date_Dialog_ID=0; 
    int cDay,cMonth,cYear; // this is the instances of the current date 
    Calendar cDate; 
    int sDay,sMonth,sYear; // this is the instances of the entered date 

    int id_dialog = 1; 
    int yr, day, month = 0; 

    // interfacing back to activity 
    public interface callBack 
    { 
     public void onItemSelected(String id); 

    } 

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

     View view = inflater.inflate(R.layout.fragment_servicing, container, false); 
     txtService = (EditText) view.findViewById(R.id.txtServiceDate); 
     mot = (Button) view.findViewById(R.id.buttonMOT); 
     servicing = (Button) view.findViewById(R.id.buttonService); 
     servicing.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // this will then pass back to the activity the string hello 
       mCallBack.onItemSelected("hello"); 

       getActivity().showDialog(1); 

      } 

     }); 

     mot.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       getActivity().showDialog(1); 



      } 

     }); 

     return view; 
    } 



    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

     // This makes sure that the container activity has implemented 
     // the callback interface. If not, it throws an exception 
     try { 
      mCallBack = (callBack) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
        + " must implement OnHeadlineSelectedListener"); 
     } 
    } 



// this is an override for when the dialog is created 
    protected Dialog onCreateDialog(int id) 
    { 
     switch(id) 
     { 
     // this will return a date picker dialog if 1 is passed 
     // could use another number for another dialog 
     case 1: 
      // passes it the current date 

      return new DatePickerDialog(getActivity(), mDateSetListener, yr, month, day); 
     } 
     return null; 
    } 
    // this returns the date back that has been selected by the user 

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

       @Override 
       public void onDateSet(DatePicker view, int year, int monthOfYear, 
         int dayOfMonth) { 

        yr = year; 
        month = monthOfYear; 
        day = dayOfMonth; 

        Log.d("date selected", "year "+ yr+ " month " +month+" day "+day); 


       } 


      }; 

} 

回答

0

您可以在EDITTEXT onClicklistner設置DateListner ..

import java.util.Calendar; 

import android.app.Activity; 
import android.app.DatePickerDialog; 
import android.app.DatePickerDialog.OnDateSetListener; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.TextView; 

public class DateListener implements OnClickListener, OnDateSetListener { 

    private Activity activity; 

    private int year; 
    private int monthOfYear; 
    private int dayOfMonth; 

    private View touchedView; 

    public DateListener(Activity activity) { 
     this.activity = activity; 
     final Calendar c = Calendar.getInstance(); 
     this.year = c.get(Calendar.YEAR); 
     this.monthOfYear = c.get(Calendar.MONTH); 
     this.dayOfMonth = c.get(Calendar.DAY_OF_MONTH); 
    } 

    public int getYear() { 
     return year; 
    } 
    public int getMonth() { 
     return monthOfYear; 
    } 
    public int getDay() { 
     return dayOfMonth; 
    } 

    public void onDateSet(DatePicker view, int year, int monthOfYear, 
      int dayOfMonth) { 
     this.year = year; 
     this.monthOfYear = monthOfYear + 1; 
     this.dayOfMonth = dayOfMonth; 

     updateDisplay(); 
     updateEditText(); 
    } 

    @Override 
    public void onClick(View v) { 
     touchedView = v; 

     new DatePickerDialog(activity, 
       this, this.getYear(), this.getMonth(), this.getDay()).show(); 
    } 

    private void updateDisplay() { 
     ((TextView) touchedView).setText(
      new StringBuilder() 
        .append(pad(dayOfMonth)).append(".") 
        .append(pad(monthOfYear)).append(".") 
        .append(pad(year))); 
    } 

    private void updateEditText() { 
     ((EditText) touchedView).setText(
      new StringBuilder() 
        .append(pad(dayOfMonth)).append(".") 
        .append(pad(monthOfYear)).append(".") 
        .append(pad(year))); 
    } 

    private static String pad(int c) { 
     if (c >= 10) 
      return String.valueOf(c); 
     else 
      return "0" + String.valueOf(c); 
    } 

} 

在你的片段

DateListener dateListener = new DateListener(getActivity()); 

edittext.setOnClickListener(dateListener);

+0

感謝您的回覆,updateDisplay和updateEditText方法不起作用((EditText)touchedView)似乎是問題所在。有任何想法嗎? – Lockey

+0

如果View是TextView,則更新顯示用於TexTView。您正在使用Edittext。你得到什麼錯誤? –

+0

在XML還設置, 機器人:可調焦=「假」 安卓的inputType =「日期」 我已經更新了我的答案。請嘗試..或者告訴我你的logcat –

相關問題