2015-07-22 58 views
-1

我想讓Date對話框選擇器自動填充我的EditText。 有2個EditText,我不知道在我的監聽器中如何編輯文本lanch dialod。片段中的日期選擇器對話框

public class DatePickerFragment extends DialogFragment{ 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    super.onCreateDialog(savedInstanceState); 
    Calendar c = Calendar.getInstance(); 
    int year= c.get(Calendar.YEAR); 
    int month = c.get(Calendar.MONTH); 
    int day_of_month = c.get(Calendar.DAY_OF_MONTH); 
    Log.i(Acceuil.APP_TAG, "DialogFrag method Id: " +this.getId()); 

    DatePickerDialog datePicker = new DatePickerDialog(getActivity(), STYLE_NORMAL, (OnDateSetListener)getActivity(), year,month,day_of_month); 

    return datePicker; 
} 

而且在我的活動:

public class AddCar extends FragmentActivity implements OnItemSelectedListener,OnDateSetListener{ 
    EditText registration_ET=null; 
    EditText control_ET=null; 
    DialogFragment newFrag= null; 
    DialogFragment newFrag2= null; 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.addcar_layout); 
     registration_ET = (EditText) this.findViewById(R.id.firstRegistration_editText); 
     registration_ET.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Log.i(Acceuil.APP_TAG,"Registration ET clicked!"); 
       newFrag = new DatePickerFragment(); 
       newFrag.show(getSupportFragmentManager(), "DateRegisterId"); 
      } 
     }); 
     control_ET = (EditText) this.findViewById(R.id.control_editText); 
     control_ET.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       Log.i(Acceuil.APP_TAG,"Registration ET clicked!"); 
       newFrag2 = new DatePickerFragment(); 
       newFrag2.show(getSupportFragmentManager(), "controlId"); 
      } 
     }); 
    } 
    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) { 
     Log.i(Acceuil.APP_TAG, "onDateSet AddCar method"); 
     //switch (view.getId()) 
     SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); 
     Calendar c = Calendar.getInstance(); 
     c.set(year, monthOfYear, dayOfMonth); 
     String date = sdf.format(c.getTime()); 

     } 

我要的是更新正確EDITTEXT registration_ET或control_ET depent whitch片段設定日期。 謝謝。

回答

0

只需更改您的DatePickerFragment如下。

public class DatePickerFragment extends DialogFragment { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     super.onCreateDialog(savedInstanceState); 
     Calendar c = Calendar.getInstance(); 
     int year = c.get(Calendar.YEAR); 
     int month = c.get(Calendar.MONTH); 
     int day_of_month = c.get(Calendar.DAY_OF_MONTH); 
     DatePickerDialog dialog = new DatePickerDialog(getActivity(), STYLE_NORMAL, (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day_of_month); 
     dialog.getDatePicker().setTag(getTag()); 
     return dialog; 
    } 
} 

注:

我加入了下面兩行代碼。

DatePickerDialog dialog = new DatePickerDialog(getActivity(), STYLE_NORMAL, (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day_of_month); 
dialog.getDatePicker().setTag(getTag()); 

現在您可以從onDateSet方法中獲得您的標記,如下所示。

@Override 
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
    if (view.getTag().equals("DateRegisterId")) { 
     registration_ET.setText(monthOfYear + "/" + dayOfMonth + "/" + year); 
    } else if (view.getTag().equals("controlId")) { 
     control_ET.setText(monthOfYear + "/" + dayOfMonth + "/" + year); 
    } 
} 

更新

1. DatePickerFragment.java

package com.example.androidtester; 

import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.os.Bundle; 
import android.support.v4.app.DialogFragment; 

import java.util.Calendar; 

public class DatePickerFragment extends DialogFragment { 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     super.onCreateDialog(savedInstanceState); 
     Calendar c = Calendar.getInstance(); 
     int year = c.get(Calendar.YEAR); 
     int month = c.get(Calendar.MONTH); 
     int day_of_month = c.get(Calendar.DAY_OF_MONTH); 
     DatePickerDialog dialog = new DatePickerDialog(getActivity(), STYLE_NORMAL, (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day_of_month); 
     dialog.getDatePicker().setTag(getTag()); 
     return dialog; 
    } 
} 

2. MainActivity.java

package brightspot.com.androidtester; 

import android.app.DatePickerDialog; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.DatePicker; 
import android.widget.EditText; 


public class MainActivity extends AppCompatActivity implements DatePickerDialog.OnDateSetListener { 

    private EditText editText; 
    private EditText editText2; 

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

     editText = (EditText) findViewById(R.id.editText); 
     editText2 = (EditText) findViewById(R.id.editText2); 
     final DatePickerFragment newFrag = new DatePickerFragment(); 
     editText.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       newFrag.show(getSupportFragmentManager(), "editText"); 
      } 
     }); 

     editText2.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       newFrag.show(getSupportFragmentManager(), "editText2"); 
      } 
     }); 
    } 

    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
     if (view.getTag().equals("editText")) { 
      editText.setText(monthOfYear + "/" + dayOfMonth + "/" + year); 
     } else if (view.getTag().equals("editText2")) { 
      editText2.setText(monthOfYear + "/" + dayOfMonth + "/" + year); 
     } 
    } 
} 

我希望這會幫助你。

+0

DatePickerDialog中沒有方法setTag .. – dav

+0

setTag不在DatePickerDialog中,它在[DatePicker](http://developer.android.com/reference/android/widget/DatePicker.html)中可用,這就是爲什麼我添加'dialog.getDatePicker()。setTag(getTag());'。 – Gunaseelan

+0

我沒有看到它。感謝您的幫助我從2天開始就這麼做! – dav

相關問題