Android 系列 6.26在TabView中导航不同的活动

117 篇文章 4 订阅
6.26在TabView中导航不同的活动
问题
您要从标签视图中的活动更改为同一标签中的另一个活动。

使用要移动到的新活动替换标签页的内容视图。
讨论
当TabView中的“调用”活动通过意图调用另一个活动时,TabView将被被调用活动的视图替换。 要在TabView中显示被调用的活动,我们可以用调用活动的视图替换调用活动的视图,以便TabView保持稳定。 要实现这一点,我们需要从ActivityGroup而不是Activity扩展调用活动。
在示例6-32中,从TabView中设置了从ActivityGroup扩展的调用活动。

实例6-32。 替换选项卡中的活动

//'Calling' activity.
public class Calling extends ActivityGroup implements OnClickListener
{
Button b1;
Intent i1;
/** Called when the activity is first created.*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calling);
b1=(Button)findViewById(R.id.changeactivity);
b1.setOnClickListener();
}
public void onClick(View view) {
// This creates an intent to call the 'Called' activity
i1=new Intent(this.getBaseContext(),Called.class);
// calls the method to replace View.
replaceContentView("Called", i1);
}
// This method is used to replace the view of 'Calling' activity by 'Called' activity.
public void replaceContentView(String id, Intent newIntent) {
// Obtain the view of 'Called' activity using its Intent 'newIntent'
View view = getLocalActivityManager().startActivity(id,
newIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) .getDecorView();
//set the above view to the content of 'Calling' activity.
this.setContentView(view);
}
“被叫活动”也可以调用另一个活动(称为CalledSecond),如下所示:

}//'Called activity'
public class Called extends Activity implements OnClickListener
{
Button b1;
Intent i1;
Calling caller;
/** Called when the activity is first created.*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.called);
b1=(Button)findViewById(R.id.changeactivity);
b1.setOnClickListener();
}
public void onClick(View view) {
// This creates an intent to call the 'CalledSecond' activity
i1=new Intent(this.getBaseContext(),CalledSecond.class);
/* 'CalledSecond' can be any activity, even the
* 'Calling'(In case backward navigation is required)
*/
// Initialize the object of the 'Calling' class.
caller=(Calling)getParent();
// calls the method to replace View.
caller.replaceContentView("CalledSecond", i1);
}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值