2013-06-14 3 views
1

에서 두 개의 textview가 있습니다. 내가 그들 클릭하면 그들은 일을 할 것입니다 .. 내 텍스트 뷰의 텍스트 선택한 날짜를 설정하려면 ... DateDialogBox이 ..이 나에게 완벽한 실행Textview 텍스트 설정 정적 내부 클래스

public class ApplyLeaveFrag extends Fragment implements OnClickListener { 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     View view = inflater.inflate(R.layout.apply_leave_fragment, container, 
       false); 

     TextView FROM = (TextView) view.findViewById(R.id.textView1); 
     TextView TO = (TextView) view.findViewById(R.id.textView2); 

     // I register setOnClickListener() on both of them: 

     FROM.setOnClickListener(this); 
     TO.setOnClickListener(this); 

     return view; 
    } 

    public void onClick(View v) { 

     switch (v.getId()) { 

     case R.id.textView1: 
      ShowCal(); 
      break; 

     case R.id.textView2: 
      ShowCal(); 
      break; 
     } 
    } 

    public void ShowCal() { 
     DialogFragment newFragment = new DatePickerFragment(); 
     newFragment.show(getFragmentManager(), "datePicker"); 

    } 

    public static class DatePickerFragment 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(this.getActivity(), this, year, month, 
        day); 
     } 

     public void onDateSet(DatePicker view, int year, int month, int day) { 
      // Do something with the date chosen by the user 
         //how can I set From or To's text From here..or any alternative 

     } 
    } 
} 
+0

textview를 정적 클래스 변수로 선언하고 onDateSet에서 동일하게 사용하십시오 – Raghunandan

+0

예 .. 왜 내가 bfr ...을 생각하지 않았는가 .. : P .. noob me .. Bt .. 나는 여전히 prob ...을 가졌습니다. uhv 언급 한 변경 .. datepickerdialog를 호출 할 수있는 두 textview 's 있습니다. 그래서 내가 어떻게 업데이트해야 하나 알 수 .. 예를 들어, 단순히 쓸 수 없습니다. FROM.setText (""+ String.valueOf (day) + "/"+ String.valueOf (월 + 1) + "/"+ String.valueOf (연도)); –

+0

및 무엇이 문제입니까? – Raghunandan

답변

1
public class ApplyLeaveFrag extends Fragment implements OnClickListener { 

    static TextView FROM,TO; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.apply_leave_fragment, container, 
     false); 
    FROM = (TextView) view.findViewById(R.id.textView1); 
    TO = (TextView) view.findViewById(R.id.textView2); 
    .... 
    } 
    ... 
    public void onDateSet(DatePicker view, int year, int month, int day) { 
    // Do something with the date chosen by the user 
    FROM.setText("from"); 
    TO.setText("TO");   

    } 
    }  

편집 : 코멘트

의 질문에 대한 클릭에

 static boolean check=true; 

변수 정적 부울 클래스를 선언 한 후

public void onClick(View v) { 

    switch (v.getId()) { 

    case R.id.textView1: 
     ShowCal(); 
     check=true; 
     break; 

    case R.id.textView2: 
     ShowCal(); 
     check=false; 
     break; 
    } 
} 

+0

이 코드는 textview에 대한 텍스트를 설정합니다. evn은 그 중 하나만을 호출합니다. –

+0

편집 사용 확인 부울 값 – Raghunandan

+0

.. 매우 많이 .. 길고 번영하다 –

1

다음을 보여줍니다!

public class ApplyLeaveFrag extends Fragment implements OnClickListener { 

static TextView FROM; 
static TextView TO; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     View view = inflater.inflate(R.layout.apply_leave_fragment, container, 
       false); 

     FROM = (TextView) view.findViewById(R.id.textView1); 
     TO = (TextView) view.findViewById(R.id.textView2); 

     // I register setOnClickListener() on both of them: 

     FROM.setOnClickListener(this); 
     TO.setOnClickListener(this); 

     return view; 
    } 

    public void onClick(View v) { 

     switch (v.getId()) { 

     case R.id.textView1: 
      ShowCal(); 
      break; 

     case R.id.textView2: 
      ShowCal(); 
      break; 
     } 
    } 

    public void ShowCal() { 
     DialogFragment newFragment = new DatePickerFragment(); 
     newFragment.show(getFragmentManager(), "datePicker"); 

    } 

    public static class DatePickerFragment 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(this.getActivity(), this, year, month, 
        day); 
     } 

     public void onDateSet(DatePicker view, int year, int month, int day) { 
      // Do something with the date chosen by the user 
         //how can I set From or To's text From here..or any alternative 
       TO.setText("Date = " + year + "/" + "month"); // and so on 

     } 
    } 
} 
관련 문제