简体   繁体   中英

fragment access widget from another fragment in the same activity

I want to change the text in text view in fragment1 by clicking a button in fragment2, i managed to do that by declaring the text view as static so i can change the text by Fragment1.textv.setText("hi"); , is that ok and what is the best way to do this.

  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所述,您需要使用接口,单击链接即可找到所需的内容。但是请注意,静态引用窗口小部件是一种不好的做法,因为在某些情况下可能会导致内存泄漏。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM