如何允许用户在 Android 应用程序中添加和删除选项卡
我正在开发一个使用选项卡的应用程序,每个选项卡都链接到一个网页,用户可以使用 webview 查看该网页并与之交互。我遇到的问题是实现一个添加和删除命令,用户可以根据需要使用该命令删除选项卡,或者添加带有自己选择的 url 的选项卡,其工作方式与其他选项卡一样。
下面是我的代码。
这是所有其他文件使用的主要 java 文件:
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 文件中定义每个网页 下面是其中之一:
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 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.
Below is my code.
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;
}
}
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 Mohammed 提到的代码可能很有用,但您可能需要
在调用之前
根据 http://code.google.com/p/android/issues/detail?id=2772。
然后您的选项卡可能会被删除,但您在尝试切换或添加选项卡时可能会注意到错误。 调用后(添加选项卡后),这个问题得到了解决
对我来说,在for 循环内部
。所以你应该得到:
希望它会有所帮助。
Using code mentioned by Mohammed can be useful, but you may need to add
before calling
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
inside the for-loop (after adding the tab).
So you should get:
Hope it will help.
您可以使用这种方式:
1-将每个选项卡添加到列表数组
2-要删除特定选项卡,请首先将其从列表中删除,然后从 tabHost 中清除所有选项卡,然后从列表中再次添加它们。
检查此代码,其中包含如何动态从 tabHost 删除选项卡的基本概念:
资源:
http://www.coderanch.com/t/460859/Android /Mobile/TabHost-Remove-Tab
我希望这会有所帮助。
M。
You may use this way:
1- add every tabs to list array
2-to remove a spesific tab, first remove it from list and clear all tabs from tabHost and add them again from list.
check this code which has basic idea of how to delete tabs from tabHost dynamically :
resource:
http://www.coderanch.com/t/460859/Android/Mobile/TabHost-Remove-Tab
I hope this will help.
M.