1 import android.os.Bundle; 2 import android.app.Activity; 3 import android.app.Fragment; 4 import android.app.FragmentManager; 5 import android.view.Menu; 6 import android.view.View; 7 import android.widget.TextView; 8 import android.widget.Toast; 9 10 /** 11 * 12 * 有3个fragment,当点击按钮时,显示第二个fragment的textView的内容 13 * @author qq 14 * 15 */ 16 public class MainActivity extends Activity { 17 18 private FragmentManager manager;//fragment管理器 19 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 } 25 26 public void showInfo(View v) 27 { 28 //获取碎片管理器----可以直接在 activity获取到 29 manager = getFragmentManager(); 30 31 //使用碎片管理器得到需要的碎片对象 32 Fragment fragment = manager.findFragmentById(R.id.fragment1); 33 34 //得到碎片对象中的布局对象 35 View view = fragment.getView(); 36 37 TextView textView = (TextView)view; 38 39 Toast.makeText(this, textView.getText(), Toast.LENGTH_SHORT).show(); 40 41 } 42 43 44 45 }
MainActivity
1 import com.example.gp08_day23_fragment2.R; 2 3 import android.app.Fragment; 4 import android.os.Bundle; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 9 10 public class TestFragment extends Fragment { 11 12 13 @Override 14 public View onCreateView(LayoutInflater inflater, ViewGroup container, 15 Bundle savedInstanceState) { 16 17 return inflater.inflate(R.layout.fragment_layout, null); 18 } 19 20 }
TetsFragment
时间: 2024-10-14 01:35:59