簡體   English   中英

來自同一活動中另一個片段的片段訪問小部件

[英]fragment access widget from another fragment in the same activity

我想通過單擊fragment2中的按鈕來更改fragment1中文本視圖中的文本,我設法通過將文本視圖聲明為static來做到這一點,因此可以通過Fragment1.textv.setText("hi");來更改文本Fragment1.textv.setText("hi"); ,可以嗎,這樣做的最佳方法是什么?

  static  textv ;
@Override
public View onCreateView(  LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
   final   View v = inflater.inflate(R.layout.fragment_frag_beam_rec, container, false);

    textv= (TextView)v.findViewById(R.id.textview);

           return v;
}

如user3806331所述,您需要使用接口,單擊鏈接即可找到所需的內容。但是請注意,靜態引用窗口小部件是一種不好的做法,因為在某些情況下可能會導致內存泄漏。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM