[英]Performing actions in fragments
我在理解片段如何與活動交互方面有些麻煩。
我的主要活動是添加片段。 調用的片段類在這里:
public class Timer_fragment extends android.support.v4.app.Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.timer_frag, container, false);
}
}
被誇大的布局里面有一個textView,但是在片段類里面我不能“訪問”它。
為什么我不能添加TextView text = (TextView) findViewById(R.id.pwd_status);
在退貨單下面?
這對我來說很有意義,因為如果我想在多個地方添加計時器並以編程方式更改一件東西,那么它將在任何地方改變。 希望這是有道理的。
謝謝
我只是保持onCreateView不變,並添加了onActivityCreated。 這樣我就可以照常做所有事情。
您可以這樣做:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.timer_frag, container, false);
TextView text = (TextView) v.findViewById(R.id.pwd_status);
/* set the TextView's text, click listeners, etc. */
return v;
}
假設您在timer_frag
布局中有一個ID為pwd_status
的TextView
,這將起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.