我的作业

我的作业

我的跳转作业

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:id="@+id/show">

</LinearLayout>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Show nextpage"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:id="@+id/btnshow"
    android:onClick="onClick"/>
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="100dp"
    android:orientation="vertical"
    android:id="@+id/txtshow2">
</LinearLayout>
fragment需要两个来进行跳转 第一个:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.tiaozhuan.FirstFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="This is first fragment!"/>
</FrameLayout>
第二个:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.administrator.tiaozhuan.SecondFragment">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="This is second fragment!"/>
</FrameLayout>
下面是主界面java代码 这些都是看同学的修改的。。 public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private Fragment firstFragment; private Fragment secondFragment; private boolean skip = true; private boolean exit = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

blankFragment = new firstFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.main, blankFragment);
transaction.commit();

}

public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_main:
jump();
break;
}
}

private void jump() {
boolean exit = true;

if (skip) {
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    if (secondFragment == null) {
         secondFragment = new SecondFragment();
        transaction.replace(R.id.main, secondFragment);
        transaction.commit();
        skip = false;
    } else {
        transaction.replace(R.id.main, secondFragment);
        transaction.commit();
        skip = false;
    }
} else {
    Toast.makeText(this, "This is second fragment!", Toast.LENGTH_LONG).show();
}

}

public boolean onKeyDown(int keyCode, KeyEvent event) {

if (event.getKeyCode() == KeyEvent.KEYCODE_BACK && exit) {

    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    skip = true;
    exit = false;
    transaction.replace(R.id.main, blankFragment);
    transaction.commit();
    return false;
} else {
    finish();
}
return super.onKeyDown(keyCode, event);

}
}

posted @ 2017-04-18 21:28  谢卓  阅读(171)  评论(0编辑  收藏  举报