安卓。如何更改选项卡中的活动
以下情况: 我有 TabActivity,例如三个选项卡:TabA、TabB、TabC。
TabC 的 Activity (Act_C_1) 中有一个按钮。因此,如果用户单击该按钮,TabC 中应该会发生另一个活动 (Act_C_2)。
我预先感谢您的任何建议/或想法。
Mur
UPD:
这是我的代码
TabActivity,具有三个活动:
public class TabScreen extends TabActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_menu);
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this, SecondActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("tab_1").setIndicator("Tab1",null).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ThirdActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("tab_2").setIndicator("Tab2",null).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, FourthActivity.class);
spec = tabHost.newTabSpec("tab_3").setIndicator("Tab3",null).setContent(intent);
tabHost.addTab(spec);
}
}
活动“Act_C_1”或 FourthActivity.java:
public class FourthActivity extends Activity implements OnClickListener
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fourth);
Button BtnWeiter = (Button)findViewById(R.id.BtnWeiter);
BtnWeiter.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
// I also tried to use LocalActivityManager
// TabActivity parentTabActivity = (TabActivity) getParent();
// LocalActivityManager manager = parentTabActivity.getLocalActivityManager();
// manager.destroyActivity("tab_3", true);
// manager.startActivity("tab_3", new Intent(this, FourthActivity.class));
finish();
startActivity(new Intent(this, FourthActivity.class));
}
}
Following situation:
I have TabActivity with e.g. three tabs, TabA, TabB, TabC.
There are a button in activity (Act_C_1) of TabC. So if the user clicks on that button, another activity (Act_C_2) should occur in TabC.
I thank you in advance for any suggestions / or ideas.
Mur
UPD:
Here is my code
TabActivity with three Activities:
public class TabScreen extends TabActivity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_menu);
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
intent = new Intent().setClass(this, SecondActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("tab_1").setIndicator("Tab1",null).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ThirdActivity.class);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("tab_2").setIndicator("Tab2",null).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, FourthActivity.class);
spec = tabHost.newTabSpec("tab_3").setIndicator("Tab3",null).setContent(intent);
tabHost.addTab(spec);
}
}
Activity 'Act_C_1' or FourthActivity.java:
public class FourthActivity extends Activity implements OnClickListener
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fourth);
Button BtnWeiter = (Button)findViewById(R.id.BtnWeiter);
BtnWeiter.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
// I also tried to use LocalActivityManager
// TabActivity parentTabActivity = (TabActivity) getParent();
// LocalActivityManager manager = parentTabActivity.getLocalActivityManager();
// manager.destroyActivity("tab_3", true);
// manager.startActivity("tab_3", new Intent(this, FourthActivity.class));
finish();
startActivity(new Intent(this, FourthActivity.class));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
可以通过以下方式切换选项卡中的活动。
首先让我们了解流程:
我们在 Tab 主机中有一个 Activity(例如一个列表),我们需要从中转到同一选项卡下的下一个 Activity(例如所单击项目的详细信息)。为此,我们可以使用替换活动的概念。还可以为所选选项卡和其他选项卡设置标志,以了解现在正在显示详细信息
当我们按回键时,我们应该在同一选项卡下获取上一个活动。为此我们可以在使用所选选项卡的特定标志时刷新选项卡,而不是再次替换活动。另外,如果显示详细信息的标志为 true,我们将转到同一选项卡中的列表,否则我们将转到选项卡小部件之前的活动(正常使用 onBackPressed)
。代码可以如下所示。
(这可以在 onClickListener 中)
按下后退完成后,我们会覆盖选项卡下每个 Activity 中的 BackPressed,以从详细信息屏幕再次转到列表
<前><代码>@Override
公共无效onBackPressed(){
// TODO 自动生成的方法存根
super.onBackPressed();
如果(MathHelper.SHOW_DETAILS){
Log.e("返回", "按下接受");
常量.LIST_ACTIVITY = 1;
Constants.SHOW_DETAILS = false;
Intent意图 = new Intent(this, Tab_widget.class);
启动活动(意图);
结束();
}
}
这里最重要的部分是
常量.LIST_ACTIVITY = 1;它指示我们所在的选项卡。因此相应的活动的值为 0,1,2...等
刷新选项卡活动时再次加载正确的列表(Activty),我们必须将其包含在 TabWidget onCreate 中创建选项卡后
The Activities in the tab can be switched in the following manner.
First Let us understand the flow:
We have in a Tab host , activity (say a list) from which we need to go to the next Activity (say details for the clicked item) under the same tab. For this we can use the concept of replacing the activity.Also setting the flags for the tab selected and other for knowing that details are being shown now
When we press back we should get the previous activity under the same tab.For this instead of again replacing the activity we can refresh the tab while using the particular flag for tab which was selected. Also if flag for show details is true we'll go the the list in the same tab or else we will go the activity before the tabwidget (normal use of onBackPressed)
The code can be as follows..
(This can be in the onClickListener)
When back pressed is done we override on BackPressed in each of the Activity under the tab to go to the list again from the details screen
The most important part here is
Constants.LIST_ACTIVITY = 1; it indicates which tab we are in. so the corresponding activities will have its value as 0,1,2...etc
Again to load the correct list (Activty) when the tab activity is refreshed we have to include this in the TabWidget onCreate after the creation of the tabs
只需使用 Intent.FLAG_ACTIVITY_CLEAR_TOP 标志来替换必要选项卡的活动。
Just use Intent.FLAG_ACTIVITY_CLEAR_TOP flag to replace an activity for necessary tab.
只需为所有选项卡添加以下内容:
Just add this for all tabs:
处理按钮的 onClick 事件
finish() 活动
添加 startActivity 方法来启动另一个活动。
谢谢
Handle the onClick event for button
finish() the activity
Add startActivity method to start another activity.
Thanks