2014-01-21 29 views
2

我正在開發與導航抽屜的應用程序。當我點擊導航列表中的一些項目時,會顯示一些使用片段的界面。和我的點擊項目也正常工作。我使用圖像按鈕來顯示日期選擇器。我使用下面的代碼。但是當我從數據選擇器對話框設置日期。它不會更改給定的文本視圖。請指導我提供了一些代碼示例..與片段類android日期選擇器

public class FindPeopleFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener { 

    public int year; 
    public int month; 
    public int day; 
    static final int DATE_PICKER_ID = 1111; 
    TextView curentDate; 




public FindPeopleFragment(TextView date){ 
    curentDate = date; 
} 
public FindPeopleFragment(){ 

} 



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

    View rootView = inflater.inflate(R.layout.schedule, container, false); 
    ImageButton date = (ImageButton) rootView.findViewById(R.id.btnDate); 
    curentDate = (TextView) rootView.findViewById(R.id.txtCurrentDate); 

    /*final Calendar c = Calendar.getInstance(); 
    year = c.get(Calendar.YEAR); 
    month = c.get(Calendar.MONTH); 
    day = c.get(Calendar.DAY_OF_MONTH); 

    curentDate.setText(new StringBuilder() 
    // Month is 0 based, just add 1 
    .append(month + 1).append("-").append(day).append("-") 
    .append(year).append(" "));*/ 


    date.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      //Toast.makeText(getActivity(), "clicked", Toast.LENGTH_LONG).show(); 
      //showDialog(DATE_PICKER_ID); 
      showDatePickerDialog(v); 
     } 
    }); 
    return rootView; 


} 

public Dialog onCreateDialog(Bundle savedInstanceState) { 


    final Calendar c = Calendar.getInstance(); 
    year = c.get(Calendar.YEAR); 
    month = c.get(Calendar.MONTH); 
    day = c.get(Calendar.DAY_OF_MONTH); 
    return new DatePickerDialog(getActivity(), datePickerListener, year, month, day); 

} 
private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener(){ 
public void onDateSet(DatePicker view, int syear, int smonth, int sday) { 
    // Do something with the date chosen by the user 
    month = smonth; 
    day = sday; 
    year = syear; 
    curentDate.setText(String.valueOf(month + 1) + "/" + String.valueOf(day) + "/" + String.valueOf(year)); 

} 

}; 

public void showDatePickerDialog(View v) { 
    DialogFragment newFragment = new DatePickerFragment(); 
    newFragment.show(getFragmentManager(), "datePicker"); 
} 
@Override 
public void onDateSet(DatePicker arg0, int arg1, int arg2, int arg3) { 
    // TODO Auto-generated method stub 

} 

}

回答

2

創建使用參數構造函數是一個壞主意。

使用界面作爲回調活動,並將選定的日期設置爲活動或折衷中的textview。

從DialogFragment到活動

How to transfer the formatted date string from my DatePickerFragment?

活動到碎片

Send data from activity to fragment in android

DialogFragment - >活動 - >片段

http://developer.android.com/training/basics/fragments/communicating.html

+0

ħ我不在這裏使用活動。當用戶在導航列表上按下一個項目時,它只顯示這個片段類。我怎樣才能做到這一點只有沒有活動的片段 –

+0

@GishanthaDarshana你不能沒有活動的片段。片段由一個活動託管。 SO通信日期到活動。然後活動分片。 MainActivity是託管framgents的主要活動。你將有一個容器,你只需添加或替換framgents。 – Raghunandan

+0

可以請你告訴我該怎麼做..我不知道片段和所有..我知道的是片段類膨脹我的佈局。我可以處理片段中的點擊事件。我在一個片段中做了某些事情。它有一個按鈕,當它被按下時,它會調用一些Web服務並執行一些任務。你能否更詳細地解釋一下 –

相關問題