如何访问片段中的UI元素?

DP_*_*DP_ 3 android android-fragments

我有一个带有id的控件(TextView)的片段,R.id.currentCycleIndicator并希望以编程方式从片段中设置其值.

都

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

返回null.

如何在片段代码中引用TextView?

Ara*_* GM 10

尝试在Fragment的onCreateView()callBack函数中获取其值,如下所示:

@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;
}
Run Code Online (Sandbox Code Playgroud)