安卓作业之跳转2

首先依旧是最基础的xml代码

<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="@string/btnshow"

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>

然后是java

View view = inflater.inflate(R.layout.fragment_biaoti, container, false);

TextView txtContent = (TextView) view.findViewById(R.id.biaoti_show);

txtContent.setText(content);

return view;

public void setContent(String content) {

this.content = content;

}

静态

FragmentManager manager = getFragmentManager();

transaction = manager.beginTransaction();

 biaotiFragment.setContent("This is first fragment");

transaction.add(R.id.show, biaotiFragment);

 transaction.commit();

动态代码

btshow.setOnClickListener(new View.OnClickListener()

{

@Override public void onClick(View view)

{

if (btshow.isClickable()

) {

 FragmentManager manager = getFragmentManager();

transaction = manager.beginTransaction();

biaotiFragment = new BiaotiFragment();

biaotiFragment.setContent("This is second fragment");

 transaction.replace(R.id.show, biaotiFragment);

 transaction.commit();

}

btshow.setOnClickListener(new View.OnClickListener()

{

 

@Override public void onClick(View view)

{ if (btshow.isClickable())

{

 

 FragmentManager manager = getFragmentManager();

transaction = manager.beginTransaction();

contentFragment.setContent("This is in second fraqment");

transaction.replace(R.id.txtshow2, contentFragment);

transaction.commit();

}

}

}

);

}

}

);

}

private long exitTime = 0;

@Override public boolean onKeyDown(int keyCode, KeyEvent event)

{

if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN)

{

if ((System.currentTimeMillis() - exitTime) > 2000)

{

 FragmentManager manager = getFragmentManager();

transaction = manager.beginTransaction();

biaotiFragment = new BiaotiFragment();

biaotiFragment.setContent("This is first fragment");

transaction.replace(R.id.show, biaotiFragment);

contentFragment=new ContentFragment();

contentFragment.setContent("");

transaction.replace(R.id.txtshow2, contentFragment);

transaction.commit();

exitTime = System.currentTimeMillis();

} else

{

 finish();

 System.exit(0);

}

return true;

}

return super.onKeyDown(keyCode, event);

}

posted @ 2017-04-18 14:18  Hero/  阅读(299)  评论(0编辑  收藏  举报