在您的主要活动中,像这样实例化您的片段类
public class MainActivity extends AppCompatActivity {
private YourFragmentClass your_fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
your_fragment = new YourFragmentClass("pass the string value here")
}
}
在您的片段类中,您可以像这样获取字符串和 setText
public class YourFragmentclass extends Fragment {
private String your_text;
public YourFragmentClass(String your_text) {
this.your_text = your_text;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = (View)inflater.inflate(R.layout.fragment_layout, container, false);
//set the text of your text view
TextView textView = (TextView) view.findViewById(R.id.text_view_id);
textView.setText(your_text);
}
}