我实现了一个tabhost,但它不希望在执行后出现

时间:2017-07-04 13:44:42

标签: android android-activity android-tabhost

我尝试从主要活动(登录屏幕)切换到登录(登录后)屏幕。我已经构建了我的简单tabhost,但如果我启动程序它不想出现。我的错在哪里?

import android.os.Bundle;
        import android.support.v7.app.AppCompatActivity;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;

public class MainActivity extends AppCompatActivity {
    // Fragment TabHost as mTabHost
    EditText text;
    Button login;
    EditText password;
    String username;
    String pw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        text = (EditText) findViewById(R.id.editText);
        login = (Button) findViewById(R.id.button);
        password = (EditText) findViewById(R.id.editText2);


        login.setOnClickListener(new View.OnClickListener(){
            public void onClick(View view){
                username =text.getText().toString();
                pw= password.getText().toString();
                System.out.println(text.getText().toString());
                System.out.println(password.getText().toString());

                if(username.equals("test") && pw.equals("test")){
                  setContentView(R.layout.logged);
                }else{
                    System.out.println("Fail");
                }

            }

        });


    }


}

logged.class

import android.os.Build;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TabHost;
import android.widget.TabWidget;

import java.util.zip.Inflater;

/**
 * Created by saddam on 04.07.2017.
 */

public class logged extends FragmentActivity {
    TabHost tabHost;
    TabWidget tw;
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Build savedInstanceState) {

        View v = inflater.inflate(R.layout.logged,container,false);

        TabHost host = (TabHost)findViewById(R.id.tabHost);
        host.setup();

        //Tab 1
        TabHost.TabSpec spec = host.newTabSpec("Tab One");
        spec.setContent(R.id.tab1);
        spec.setIndicator("Tab One");
        host.addTab(spec);

        //Tab 2
        spec = host.newTabSpec("Tab Two");
        spec.setContent(R.id.tab2);
        spec.setIndicator("Tab Two");
        host.addTab(spec);

        //Tab 3
        spec = host.newTabSpec("Tab Three");
        spec.setContent(R.id.tab3);
        spec.setIndicator("Tab Three");
        host.addTab(spec);
        return v;
    }
}

logged.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TabHost
        android:id="@+id/tabHost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

        <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"></TabWidget>

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

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#ffc916"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="This is tab 1" />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#da8200"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="This is tab 2" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#5b89ff"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:text="This is tab 3" />
                </LinearLayout>
            </FrameLayout>
        </LinearLayout>
    </TabHost>

1 个答案:

答案 0 :(得分:0)

如何使用TabHost请查看这个完整的示例,它对您非常有帮助 http://www.technotalkative.com/android-tab-bar-example-1/