本次作业个人方法
1.界面布局就很简单了,主界面用LinearLayout布局,分为两个板块,控件也只有一个button
代码如下:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/show">
</LinearLayout>
<Button
android:id="@+id/button1"
android:text="show next page"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="onClick"/>
<LinearLayout
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
第二第三个均用Fragment布局(代码如下)
<FrameLayout 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="cn.edu.niit.dazuoye.MainActivity"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="20sp"
android:text="this is the first fragment" />
</FrameLayout>
第三个界面跟第二个几乎相同
2.功能的实现:及首先点击按钮show next page出现“this first fragmant”;再次点击按钮show next page出现提示“this second fragmant”;再点击一次返回初始界面。(主要代码如下)
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Fragment oneFragment;
private Fragment twoFragment;
private boolean skip = true;
private boolean exit = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
oneFragment = new oneFragment();
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.main, oneFragment);
transaction.commit();
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
jump();
break;
}
}
private void jump() {
boolean exit = true;
if (skip) {
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (secondFragment == null) {
twoFragment = new twoFragment();
transaction.replace(R.id.main, twoFragment);
transaction.commit();
skip = false;
} else {
transaction.replace(R.id.main, secondFragment);
transaction.commit();
skip = false;
}
} else {
Toast.makeText(this, "this is second fragmant", Toast.LENGTH_LONG).show();
}
}