2013-08-13 5 views
3

ID가 R.id.currentCycleIndicator 인 컨트롤 (TextView)이있는 프래그먼트가 있으며 프래그먼트 내에서 값을 프로그래밍 방식으로 설정하려고합니다.조각에서 UI 요소에 액세스하는 방법?

  • getActivity().findViewById(R.id.currentCycleIndicator)
  • getView().findViewById(R.id.currentCycleIndicator)

는 null.

조각 코드 내에서 해당 TextView에 대한 참조를 얻으려면 어떻게해야합니까?

답변

10

아래처럼) (onCreateView에서 조각의 콜백 함수를 값을 받고보십시오 :

private TextView tv; 

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    super.onViewCreated(view, savedInstanceState); 
    tv = (TextView) view.findViewById(R.id.currentCycleIndicator); 
} 
+0

이것은 또한 onViewCreated에서 제대로 작동해야합니까? –

+0

나는 아주 오래된 스레드를 다시 여는 것을 알지만, 다른 활동에서 textView에 액세스해야한다면 어떨까요? 현재 활성 조각의 객체를 얻으려면 어떻게해야합니까? –

1

당신은 아래와 같이 onViewCreated 기능을 재정의 것을 할 수 onCreateView에서 TextView를 참조하십시오.

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
      View view = inflater.inflate(R.layout.layoutwheretextviewis, container, false); 

      TextView text= (TextView) view.findViewById(R.id.currentCycleIndicator); 
      return view; 
} 
1

내부보기를 부풀려 :

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

    View view = inflater.inflate(R.layout.fragment_layout, container, false); 

    TextView textView = (TextView) view.findViewById(R.id.currentCycleIndicator); 

    return view; 
} 
관련 문제