請問動態加載是不是不能在MainActivity中操控fragment里的組件啊
public class MainActivity extends AppCompatActivity {
? ?private Button bt;
? ?private TextView tv;
? ?@Override
? ?protected void onCreate(Bundle savedInstanceState) {
? ? ? ?super.onCreate(savedInstanceState);
? ? ? ?setContentView(R.layout.activity_main);
? ? ? ?MyFragment myFragment=new MyFragment();
? ? ? ?FragmentManager fragmentManager=getFragmentManager();
? ? ? ?FragmentTransaction beginTransaction=fragmentManager.beginTransaction();
? ? ? ?beginTransaction.add(R.id.rl,myFragment);
? ? ? ?beginTransaction.commit();
? ? ? /* bt=(Button)findViewById(R.id.button); ? ?
? ? ? ?tv=(TextView)findViewById(R.id.textView);
? ? ? ?bt.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ?@Override
? ? ? ? ? ?public void onClick(View v) {
? ? ? ? ? ? ? ?tv.setText("Fragment里的按鈕在活動中起作用了");
? ? ? ? ? ?}
? ? ? ?});*/
? ?}
}
如果不把注釋加上,程序就會崩潰,,,靜態加載就可以不用加注釋
2017-08-20
可以通過:
View view = getLayoutInflater().inflate(R.layout.fragment, null,false);?
text = (TextView) view.findViewById(R.id.text);
獲取fragment中的控件
2016-07-21
我加了程序也會崩潰~~~
2016-07-19
在Myfragment中執行對fragment的組件的操作 ? ? ? ? ? 你加注釋的部分是對activity_main中組件的操作 并沒有體現對fragment中的操作