ユーザーがアプリケーションにタブを追加および削除できるコマンドを実装する方法は?

StackOverflow https://stackoverflow.com/questions/4309964

質問

SafariやFirefoxのタブを使用するコンピューターなど、Webサイトもリンクするタブを上にあるアプリを作成しようとしています。私がやろうとしているのは、ユーザーが必要に応じてタブを削除し、別のWebサイトにリンクする別のタブを追加できるADDおよび削除クラスを実装することです。どんな助けも大歓迎です。

これがメインの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ファイルが使用するJavaファイルの1つです。

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

新しいタブが追加されたときに、この同じ形式を作成するために、追加および削除ボタンを実装するにはどうすればよいですか。

役に立ちましたか?

解決

目的の効果を達成するには、おそらく各タブアプローチのアクティビティをドロップする必要があります。

代わりに、URLをパラメーターとして受け入れるすべてのWebサイトに使用できる「デフォルト」ビューを作成します。

次に、各タブに新しいアクティビティを「意図」する代わりに、そのビューを新しいタブに膨らませ、URLをパラメーターとして渡します。

タブを作成して削除するのは簡単です。ビューを膨らませたり削除したりする簡単なボタンは、控えめになります。


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コード

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;
    }  
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top