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

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

问题描述

我正在开发一个使用选项卡的应用程序,每个选项卡都链接到用户将能够使用 webview 看到并与之交互的网页.我遇到的问题是实现一个添加和删除命令,如果需要,用户可以使用该命令删除一个选项卡,或者添加一个带有他们选择的 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.

下面是我的代码.

这是所有其他文件使用的主要 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.

推荐答案

使用 Mohammed 提到的代码可能有用,但您可能需要添加

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

tabs.setCurrentTab(0);

打电话之前

tabs.clearAllTabs();

根据 http://code.google.com 中描述的问题/p/android/issues/detail?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***
}

希望它会有所帮助.

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

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