2017-02-28 7 views
2

으로 선언 된 메소드에서 값을 가져온다 안녕하세요 저는 Android와 Java에 익숙하지 않습니다. DialogFragment을 확장하고 DatePickerDialog.OnDateSetListener을 구현하는 Date Picker Fragment 클래스를 작성 중입니다.이 메소드는 public void onDateSet(DatePicker view, int year, int month, int day)인데이를 선언해야합니다. 새 날짜를 publicString 변수로 설정하십시오.public void

주된 활동에 public String 변수를 사용하면 TextView에 값을 표시 할 수 있습니까? 그렇다면 어떻게?

내 주요 활동 코드는 다음과 같습니다

public class TARForm extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { 

    EditText departuredate; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_tarform); 
     departuredate = (EditText) findViewById(R.id.departure_date); 
    } 

    public void showDatePickerDialog(View v) { 
     DialogFragment newFragment = new DatePickerFragment(); 
     newFragment.show(getFragmentManager(), "datePicker"); 
    } 
} 

그리고DatePickerFragment코드 것은 :

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener{ 

    int selectyear, selectmonth, selectday; 
    public String date; 

    @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); 
     return new DatePickerDialog(getActivity(), this, year, month, day); 
    } 

    public void onDateSet(DatePicker view, int year, int month, int day) { 

     selectday = day; 
     selectmonth = month+1; 
     selectyear = year; 
     date = selectday + "-" + selectmonth + "-" + selectyear; 
    } 
} 

당신은 내가 "날짜를 얻을 수있는 방법이 정보로 안내 할 수있는 경우 "MainActivity로 출발 했습니까? 감사.

+0

주 활동 및 단편 클래스의 코드를 공유하고 있습니다. 도와주세요. – rodaxyz

+0

당신은'OnDismissListener'를 사용할 수 있습니다, 이것을 확인해야합니다 [답변] (http://stackoverflow.com/a/23786151/5769824) – Hmm

+0

인터페이스/콜백 사용 – Rahul

답변

0
//In your actiivty 
String date=null; 

public void setDate(String date){ 
    this.date=date; 
    //set this date in your text View 
} 


    //In your fragment 

Context context; 
@Override 
public void onAttach(Context context){ 
    this.context=context; 
} 

public void onDateSet(DatePicker view, int year, int month, int day){ 
    String date=//get Date over here 
    ((TARForm)context).setDate(date) 
} 
+0

예를 들어 설명 할 수 있습니까? 기계적 인조 인간. – rodaxyz

+0

내 대답을 편집했습니다. 지금 그 모습을 살펴보십시오. –

+0

지금 내 코드를 확인할 수 있습니다. 감사합니다 – rodaxyz

0

당신은 당신의 활동하는 경우

public void onDateSet(DatePicker view, int year, int month, int day) { selectday = day; selectmonth = month+1; selectyear = year; date = selectday + "-" + selectmonth + "-" + selectyear; if(getActivity() instanceOf TARForm){ ((TARForm)getActivity()).departuredate.setText(date); } } 

하고 TARForm 활동

조각 다른에 public EditText departuredate;onDateSet() 방법 안에 당신의 조각 코드 아래에 시도 할 수 있습니다 폴더

+0

하시기 바랍니다. 코드와 도움말을 참조하십시오. 감사합니다 – rodaxyz

+0

내 대답을 업데이트했습니다, 확인하십시오 – ShekharKG

+0

고마워. – rodaxyz