frament
在第一个界面first frgment中点击按钮show next page将跳转进入第二个界面second fragent,再次单击按钮show next page跳转进入第三个界面first fragment.首先做好三个布局界面的设计,其次再运用java来实现三个界面的跳转。
第一个主要布局代码如下
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.mpyypm.chacha1.FragmentActivity">
<LinearLayout
android:id="@+id/W"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="3">
<Button
android:id="@+id/action0"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="SHOW NEXT PAGE"
android:textSize="25dp"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:onClick="onClick"
/></LinearLayout>
</LinearLayout>
第二个主要代码:
<LinearLayout
android:id="@+id/w"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:layout_weight="1">
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Buttonandroid:id="@+id/action0"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/action0"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:onClick="onClick"
/>
</LinearLayout>
第三段代码:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_light"
android:gravity="center"
android:textSize="25sp"
android:text="@string/first_fragment" />
</FrameLayout>
第四段代码:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorAccent"
android:gravity="center"
android:text="@string/second_fragment"
android:textSize="25sp" /></FrameLayout>
第五段代码:
public class FragmentActivity extends AppCompatActivity implements View.OnClickListener{
FirstFragment firstFragment;
SecondFragment secondFragment;
private boolean a=true;
boolean e=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
firstFragment = new FirstFragment();
transaction.add(R.id.show,firstFragment);
transaction.commit();
}
@Override
public void onClick(View view) {
if(view.getId()==R.id.action0){
e=true;
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (secondFragment == null){
secondFragment = new SecondFragment();
transaction.replace(R.id.show,secondFragment);
transaction.commit();
a=false;
} else{
transaction.replace(R.id.show,secondFragment);
transaction.commit();
a=false;
}
}else{
Toast.makeText(this,"This is second fragment",Toast.LENGTH_SHORT).show();
}
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getKeyCode()==KeyEvent.KEYCODE_BACK&&e){
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
a=true;
e=false;
transaction.replace(R.id.show,firstFragment);
transaction.commit();
return false;
} else {
finish();
}
return super.onKeyDown(keyCode, event);
}
}
浙公网安备 33010602011771号