如何实现一个命令,允许用户在应用程序中添加和删除标签? [英] How to implement a command that allows user to add and delete tabs in the application?

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

问题描述

我试图与标签的应用程序在链接一个网站也各一个,如在使用Safari或Firefox标签的计算机上。我所要做的是实现一个添加和删除类,将允许用户如果想删除一个选项卡并添加另一个会链接到其他网站。任何帮助将大大AP preciated。

下面是主要的Java文件。

 公共类UniversityofColorado扩展TabActivity {
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    TabHost主机= getTabHost();    host.addTab(host.newTabSpec(1)
            .setIndicator(谷歌)
            .setContent(新意图(这一点,Hello.class)));    host.addTab(host.newTabSpec(二)
                    .setIndicator(科罗拉多州主网站)
                    .setContent(新意图(这一点,ColoradoMainSiteBrowser.class)));    host.addTab(host.newTabSpec(三化)
                    .setIndicator(CULearn)
                    .setContent(新意图(这一点,CULearnBrowser.class)));    host.addTab(host.newTabSpec(四)
            .setIndicator(CULink)
            .setContent(新意图(这一点,CULinkBrowser.class)));    host.addTab(host.newTabSpec(十二五)
            .setIndicator(MyCUInfo)
            .setContent(新意图(这一点,MyCUInfoBrowser.class)));    host.addTab(host.newTabSpec(六个一)
            .setIndicator(校园地图)
            .setContent(新意图(这一点,CampusBrowser.class)));    host.addTab(host.newTabSpec(七)
            .setIndicator(「票据」)
            .setContent(新意图(这一点,Notepadv3.class)));
}
    //膨胀菜单时菜单键是pressed
    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        MenuInflater吹气= getMenuInflater();
        inflater.inflate(R.menu.menu,菜单);
        返回true;
    }
}

这是java文件之一,主要的java文件使用:

 公共类ColoradoMainSiteBrowser延伸活动{的WebView的WebView;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
网页流量=(的WebView)findViewById(R.id.webview);
webview.setWebViewClient(新HelloWebViewClient());
webview.getSettings()setJavaScriptEnabled(真)。
webview.loadUrl(http://colorado.edu/);
}
私有类HelloWebViewClient扩展WebViewClient {
@覆盖
公共布尔shouldOverrideUrlLoading(的WebView视图,字符串URL)
{
view.loadUrl(URL);
返回true;
}
}
公共布尔的onkeydown(INT键code,KeyEvent的事件){
如果((键code == KeyEvent.KEY code_BACK)及和放大器; webview.canGoBack()){
webview.goBack();
返回true;
}
返回super.onKeyDown(键code,事件);
}
}

我将如何贯彻落实添加和删除按钮来设定此相同的格式,当添加新的选项卡。


解决方案

在为了达到预期的效果,你可能需要删除一个活动为每个标签的方法。

相反,你可以创建你可以用它来接受URL作为参数的所有网站的默认的看法。

然后,而不是intenting每个标签一个新的活动,你认为充气以新标签,传递URL作为参数。<​​/ P>

要创建和删除dinamically标签很容易。一个简单的按钮膨胀/删除视图将sufice。


XML

RES /布局/ main.xml中

 &LT; TabHost的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:ID =@机器人:ID / tabhost
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT&GT;
    &LT;的LinearLayout
        机器人:方向=垂直
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =FILL_PARENT
        机器人:填充=5DP&GT;
        &LT; TabWidget
            机器人:ID =@机器人:ID /标签
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT/&GT;
        &LT;的FrameLayout
            机器人:ID =@机器人:ID / tabcontent
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =FILL_PARENT
            机器人:填充=5DP/&GT;
    &LT; / LinearLayout中&GT;
&LT; / TabHost&GT;

RES /布局/ tab.xml

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;的LinearLayout
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:方向=垂直
&GT;
&LT;的TextView
机器人:ID =@ + ID / url_tv
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=你好!TAB
&GT;
&LT; / TextView的&GT;
&LT; / LinearLayout中&GT;

RES /菜单/ menu.xml文件

 &LT;?XML版本=1.0编码=UTF-8&GT?;
&LT;菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android&GT;
&LT;项目的android:启用=真正的机器人:可见=真机器人:图标=@机器人:可绘制/ ic_menu_add机器人:标题=添加网站机器人:titleCondensed =加机器人:ID =@ + ID /添加&GT;&LT; /项目&GT;
&LT;项目的android:titleCondensed =删除机器人:启用=真正的机器人:可见=真机器人:图标=@机器人:可绘制/ ic_menu_delete机器人:ID =@ + ID /删除机器人:标题=删除网站&GT;&LT; /项目&GT;
&所述; /菜单&gt;

JAVA code

main.java

 进口android.app.TabActivity;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.MenuItem;
进口android.view.View;
进口android.widget.TabHost;
进口android.widget.TextView;
公共类主要扩展TabActivity实现TabHost.TabContentFactory {
    TabHost tabHost;    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);        this.tabHost = getTabHost(); //活动TabHost        tabHost.addTab(tabHost.newTabSpec(\"http://www.google.com/\").setIndicator(\"Google\").setContent(this));
        tabHost.addTab(tabHost.newTabSpec(\"http://www.stackoverflow.com/\").setIndicator(\"StackOverflow\").setContent(this));
    }    公共查看createTabContent(字符串标签){        //通货膨胀本身。
        //我们不返回任何东西,所以我们可以用这个观点工作
        查看选项卡= View.inflate(这一点,R.layout.tab,NULL);        //这里我们的观点合作
        //在这种情况下,我们设置的TextView以配合我们的标签
        //它来解析参数的新标签的好方法
        TextView的电视=(TextView中)tab.findViewById(R.id.url_tv);
        tv.setText(标签);        //下班后我们就可以返回查看
        返回标签;
    }
    私人无效addMethod(字符串webSiteURL,字符串webSiteName){        //找到适合您将参数传递给这里的一种方式        tabHost.addTab(tabHost.newTabSpec(webSiteURL).setIndicator(webSiteName).setContent(本));    }    私人无效deleteMethod(){        //创建一个方法来决定要删除的选项卡。
        //例如,FindViewByTag是一个ViewGroup和工作方法
        //然后调用视图与父
        //父的ViewGroup =(ViewGroup中)viewToDelete.getParent();
        //最后用oarent.removeView(viewToDelete);    }    //膨胀菜单时菜单键是pressed
    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        MenuInflater吹气= getMenuInflater();
        inflater.inflate(R.menu.menu,菜单);
        返回true;
    }
    //一旦选择了菜单调用此方法
    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        开关(item.getItemId()){            案例R.id.add:                //这样的东西传递给我们的addMethod这些变量是人为造成
                //你应该找到一种方法将这些参数传递给我们的方法
                //可以是一个对话窗口,或从什么地方获取URL
                //它的您的来电
                字符串webSiteURL =htt​​p://www.evonytools.org/;
                字符串webSiteName =Tivie的工具;                addMethod(webSiteURL,webSiteName);            案例R.id.delete:                deleteMethod();                打破;
        }
        返回true;
    }
}

I am trying to make an app with tabs at the top that link a website too each one, such as a computer that uses tabs in safari or firefox. What I am trying to do is implement an add and delete class that will allow the user to delete a tab if wanted and add another that will link to a different website. Any help would be greatly appreciated.

Here is the main java file.

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;
    }
}

This is one of the java files that the main java file uses:

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);
}
}

How would I implement the add and delete buttons to have this same format when the new tabs were added.

解决方案

In order to achieve the desired effect, you probably need to drop an activity for each tab approach.

Instead, you would create a "default" view that you can use to all websites that accept the URL as a parameter.

Then, instead of "intenting" a new activity to each tab, you Inflate that view to the new tab, passing the URL as a parameter.

To create and delete tabs dinamically would be easy. A simple button that Inflates/removes the view would sufice.


XML

res/layout/main.xml

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="5dp">
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:padding="5dp" />
    </LinearLayout>    
</TabHost>

res/layout/tab.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<TextView
android:id="@+id/url_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HELLO TAB!"
>
</TextView>
</LinearLayout>

res/menu/menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:enabled="true" android:visible="true" android:icon="@android:drawable/ic_menu_add" android:title="Add Website" android:titleCondensed="add" android:id="@+id/add"></item>
<item android:titleCondensed="Delete" android:enabled="true" android:visible="true" android:icon="@android:drawable/ic_menu_delete" android:id="@+id/delete" android:title="Delete Website"></item>
</menu>

JAVA CODE

main.java

import android.app.TabActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TextView;


public class Main extends TabActivity implements TabHost.TabContentFactory {


    TabHost tabHost;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        this.tabHost = getTabHost();  // The activity TabHost

        tabHost.addTab(tabHost.newTabSpec("http://www.google.com/").setIndicator("Google").setContent(this));
        tabHost.addTab(tabHost.newTabSpec("http://www.stackoverflow.com/").setIndicator("StackOverflow").setContent(this)); 
    }

    public View createTabContent(String tag) {

        // Inflation per se.
        // we don't return anything yet so we can work with this view
        View tab = View.inflate(this, R.layout.tab, null);

        // Here we work with the view
        // in this case we set the textview to match our tag
        // its a good way to parse arguments to the new tab
        TextView tv = (TextView) tab.findViewById(R.id.url_tv);
        tv.setText(tag);

        //After work we can return view
        return tab;  
    }


    private void addMethod(String webSiteURL, String webSiteName) {

        // Find a way that suits you to pass arguments to here

        tabHost.addTab(tabHost.newTabSpec(webSiteURL).setIndicator(webSiteName).setContent(this));

    }

    private void deleteMethod() {

        // Create a way to decide which tab to delete.
        // for instance, FindViewByTag is a method of ViewGroup and works
        // then call the parent of the view with
        // ViewGroup parent = (ViewGroup) viewToDelete.getParent();
        // finally use oarent.removeView(viewToDelete);

    }

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


    // This method is called once the menu is selected
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {


        switch (item.getItemId()) {

            case R.id.add:

                // These variable is created artificially so that something is passed to our addMethod
                // You should find a way to pass these argument to our method
                // can be a dialog screen, or getting the URL from somewhere
                // its your call
                String webSiteURL = "http://www.evonytools.org/";
                String webSiteName = "Tivie's Tools";

                addMethod(webSiteURL, webSiteName);

            case R.id.delete:

                deleteMethod();

                break;
        }
        return true;
    }  
}

这篇关于如何实现一个命令,允许用户在应用程序中添加和删除标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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