在android中如何在TabHost中以片段形式传递意图

在android中如何在TabHost中以片段形式传递意图,android,android-intent,fragment,fragment-tab-host,Android,Android Intent,Fragment,Fragment Tab Host,我正在使用Fragment,我的问题是如何在Fragment中打开类Test1和Test2。实际上,我使用意图来重定向活动,但我想使用片段而不是活动。这怎么可能 I need to two tab. 1)App 2)Application. and Successfully Create both tab. but using with fragment Like TabHost. 请推荐我。 下面是我的代码 我的密码是 import android.app.Fr

我正在使用Fragment,我的问题是如何在Fragment中打开类Test1和Test2。实际上,我使用意图来重定向活动,但我想使用片段而不是活动。这怎么可能

  I need to two tab. 1)App 2)Application. and Successfully Create both tab. but using with fragment Like TabHost. 
请推荐我。 下面是我的代码

我的密码是

            import android.app.Fragment;
            import android.os.Bundle;
            import android.view.LayoutInflater;
            import android.view.View;
            import android.view.ViewGroup;
            import android.widget.AnalogClock;
            import android.widget.TabHost;

            import com.example.app.R;

            public class CallFragment extends Fragment implements OnTabChangeListener{
                private TabHost tabHost = null;
                View rootView = null;
                TabHost.TabSpec spec, spec1;

                @Override
                public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {
                    /**
                     * Inflate the layout for this fragment
                     */
                    View rootView = inflater.inflate(R.layout.call_fragment, container,
                            false);

                    tabHost = (TabHost) rootView.findViewById(R.id.tab_host);
                    tabHost.setup();
                    spec = tabHost.newTabSpec("tagApp");
                    spec1 = tabHost.newTabSpec("tagApplication");
                    spec.setIndicator("App");
                    spec.setContent(R.id.test1);

                    spec1.setIndicator("Application");
                    spec1.setContent(R.id.test2);

                    tabHost.setOnTabChangedListener(this);
                    tabHost.addTab(spec);
                    tabHost.addTab(spec1);
                    return rootView;
                }

        @Override
            public void onTabChanged(String tabId) {
                // TODO Auto-generated method stub
                if ("tagApp".equals(tabId)) {

                    Intent intent = new Intent(getActivity(), Test1.class);
                    getActivity().startActivity(intent);
                }
                if ("tagApplication".equals(tabId)) {
                    Intent intent = new Intent(getActivity(), Test2.class);
                    getActivity().startActivity(intent);
                }
            }
            }

    Test1.java file,
    public class Test1 extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.test1);
        }
    }

    Test2.java file,
    public class Test2 extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.test2);
        }
    }

    And call_fragment.xml file is,

        <?xml version="1.0" encoding="utf-8"?>
        <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/tab_host"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical" >

                <TabWidget
                    android:id="@android:id/tabs"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal" />

                <FrameLayout
                    android:id="@android:id/tabcontent"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                 <include layout="@layout/test1" />

                 <include layout="@layout/test2" />
            </FrameLayout>
            </LinearLayout>

        </TabHost>

    test1.xml file,
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/test1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tag 1" />

    </LinearLayout>

    test2.xml file,
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/test2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Tag 2" />

    </LinearLayout>
导入android.app.Fragment;
导入android.os.Bundle;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AnalogClock;
导入android.widget.TabHost;
导入com.example.app.R;
公共类CallFragment扩展了实现OnTabChangeListener的片段{
私有TabHost TabHost=null;
视图rootView=null;
TabHost.TabSpec spec,spec1;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
/**
*为该碎片膨胀布局
*/
视图根视图=充气机。充气(R.layout.call_碎片,容器,
假);
tabHost=(tabHost)rootView.findviewbyd(R.id.tab\u host);
tabHost.setup();
spec=tabHost.newTabSpec(“tagApp”);
spec1=tabHost.newTabSpec(“tagApplication”);
规格设置指示器(“App”);
规格设置内容(R.id.test1);
规范1.设置指示器(“应用”);
spec1.setContent(R.id.test2);
tabHost.setOnTabChangedListener(此);
tabHost.addTab(spec);
tabHost.addTab(spec1);
返回rootView;
}
@凌驾
已更改的公共无效项(字符串选项卡ID){
//TODO自动生成的方法存根
如果(“tagApp.”等于(tabId)){
Intent Intent=新的Intent(getActivity(),Test1.class);
getActivity().startActivity(意图);
}
if(“tagApplication”.equals(tabId)){
Intent Intent=新的Intent(getActivity(),Test2.class);
getActivity().startActivity(意图);
}
}
}
Test1.java文件,
公共类Test1扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.test1);
}
}
Test2.java文件,
公共类Test2扩展了活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
//TODO自动生成的方法存根
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
}
}
调用_fragment.xml文件为,
test1.xml文件,
test2.xml文件,

如果预期返回值类型为“视图”,则不可能返回意图。 用不同的方法处理这个问题

对于重定向选项卡,返回一个空视图
returnnewview(/*您想要设置的任何参数*/)

然后在TabHost中注册onTabChangeListener,这样就可以对选项卡选择做出反应。 对于重定向选项卡索引,创建并启动您的意图,从而启动新活动


请参见:现有示例。

您的问题不太清楚。我的理解是:当某个选项卡被选中/激活时,您想启动另一个活动(如重定向),对吗?是的,每当单击所选选项卡时,我都想重定向。使用此代码,它是打开的新活动,但它是在片段中打开的,那么它的用途是什么?@Override public void onTabChanged(String tabId){//TODO自动生成的方法存根if(“tagApp.equals(tabId)){Intent Intent=new Intent(getActivity(),Test1.class);getActivity().startActivity(Intent);}if(“tagApplication.equals(tabId)){Intent Intent Intent=new Intent(getActivity(),Test2.class);getActivity().startActivity(Intent);}请将此代码作为更新干净地发布在您的帖子中,以便每个人都能阅读。