如何允许用户添加和删除选项卡中的Andr​​oid应用程序 [英] How to allow user to Add and Delete Tabs in an android application

查看:108
本文介绍了如何允许用户添加和删除选项卡中的Andr​​oid应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发,使用卡口与每个标签被链接到网页的用户将能够看到和使用web视图交互的应用程序。什么我有正在实施一个添加和删除命令,用户将可以使用删除选项卡如果需要的话,或添加一个标签与他们所选择的URL的作品就像其他的麻烦。

I am developing an application that uses tabs with each tab being linked to a webpage that the user will be able to see and interact with using webview. What I am having trouble with is implementing a add and delete command that the user will be able to use to delete a tab if desired or add a tab with a url of their choice that works just like the others.

下面是我的code。

下面是所有其他文件使用的主要的Java文件:

Here is the main java file that all other files use:

public class UniversityofColorado extends TabActivity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    TabHost host=getTabHost();

    host.addTab(host.newTabSpec("one")
            .setIndicator("Google")
            .setContent(new Intent(this, Hello.class)));

    host.addTab(host.newTabSpec("two")
                    .setIndicator("Colorado Main Site")
                    .setContent(new Intent(this, ColoradoMainSiteBrowser.class)));

    host.addTab(host.newTabSpec("three")
                    .setIndicator("CULearn")
                    .setContent(new Intent(this, CULearnBrowser.class)));

    host.addTab(host.newTabSpec("four")
            .setIndicator("CULink")
            .setContent(new Intent(this, CULinkBrowser.class)));

    host.addTab(host.newTabSpec("five")
            .setIndicator("MyCUInfo")
            .setContent(new Intent(this, MyCUInfoBrowser.class)));

    host.addTab(host.newTabSpec("six")
            .setIndicator("Campus Map")
            .setContent(new Intent(this, CampusBrowser.class)));

    host.addTab(host.newTabSpec("Seven")
            .setIndicator("Notes")
            .setContent(new Intent(this, Notepadv3.class)));
}   




    // Inflates menu when "menu Key" is pressed
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }
}

然后,我有一个单独的Java文件中定义的每个网页的主要文件的调用 下面就是其中之一:

Then I have each webpage defined in a separate java file that the main file calls below is one of them:

public class ColoradoMainSiteBrowser extends Activity {

WebView webview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.loadUrl("http://colorado.edu/");
}
private class HelloWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

我有我只需要构造方法的主要文件中定义这样的按钮做他们该做的菜单。任何帮助将是巨大的。

I have the menu defined in the main file I just need to construct the methods so the buttons do what they are suppose to do. Any help would be great.

推荐答案

使用由穆罕默德提到的code可能是有用的,但你可能需要添加

Using code mentioned by Mohammed can be useful, but you may need to add

tabs.setCurrentTab(0);

在调用之前

tabs.clearAllTabs();

// $ C:

根据在 HTTP说明问题$ c.google.com / P /安卓/问题/详细信息?ID = 2772 。 那么你的卡可能被删除,但尝试切换或添加标签时,您可能会注意到一个错误。对我来说,这个问题是固定的呼吁后,

according to issue described at http://code.google.com/p/android/issues/detail?id=2772. Then your tab probably gets removed, but you may notice an error when trying to switch or add tabs. For me, this problem was fixed after calling

tabs.setCurrentTab(index);

内部的for循环(添加的标签之后)。

inside the for-loop (after adding the tab).

所以,你应该得到的:

list.remove(nTabToRemoveIndex);
tabs.setCurrentTab(0);   // <== ***FIRST EDIT***
tabs.clearAllTabs();
int nTabIndex = 0;       // <== ***SECOND EDIT***
for(TabHost.TabSpec spec : list)
{
  tabs.addTab(spec);
  tabs.setCurrentTab(nTabIndex++);   // <== ***THIRD EDIT***
}

希望这将有助于。

Hope it will help.

这篇关于如何允许用户添加和删除选项卡中的Andr​​oid应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆