管理片段
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Java
public class MainActivity extends AppCompatActivity {
private Integer con = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// 方法1
// Frag1 f1 = (Frag1) fragmentManager.findFragmentById(R.id.fragment);
// 方法2
Frag1 f1 = (Frag1) fragmentManager.findFragmentByTag("tag");
Button bt = (Button) f1.getView().findViewById(R.id.button);
bt.setText("Fragmentbut");
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
con = con + 1;
Toast.makeText(MainActivity.this, "Count" + con, Toast.LENGTH_SHORT).show();
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.e560.m1014c.MainActivity">
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.e560.m1014c.Frag1"
android:tag="tag"
android:layout_weight="1" />
</LinearLayout>
0 件のコメント:
コメントを投稿