标签不起作用ANDROID有多个活动?

时间:2012-12-25 02:18:47

标签: android android-layout android-intent android-widget

  

可能重复:
  Custom Tabwiget for Android

我是Android新手。我对以下情况感到困惑。

我创建了一个按钮和该按钮的OnClick,用户被重定向到(Android XML布局选项卡,即nurse.xml)。现在重定向工作正常,

nurse.xml包含一个选项卡。活动无法显示标签,因此一切都会失真。

    btnSignIn.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View view) 
        {
            setContentView(R.layout.nurse);
        }
   }

nurse.xml的示例代码:         

    <TabHost 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/tabHost"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <TabWidget
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs" />

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

            <!--  Code for READING tab starts here -->
            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:id="@+id/READING"
                android:orientation="vertical"
                android:paddingTop="60dp">
                <TextView  
                    android:layout_width="fill_parent" 
                    android:layout_height="100dp" 
                    android:text="@string/reading_tab" />    
            </LinearLayout>

            <!--  Code for MEDICATION medication starts here -->
            <LinearLayout
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/MEDICATION"
                    android:orientation="vertical"
                    android:paddingTop="60dp" >
                <TextView  
                    android:layout_width="fill_parent" 
                    android:layout_height="100dp" 
                    android:text="@string/medication_tab" />
            </LinearLayout>
        </FrameLayout>
    </TabHost>

任何帮助都会有所帮助,我需要使用nurse.xml来运行onClick按钮。

谢谢!!!

2 个答案:

答案 0 :(得分:1)

阅读和药物应该有他们独立的xml和类。然后在你的护士主要课堂上打电话给他们。

Intent intentReading = new Intent().setClass(this, Reading.class);
        TabSpec tabReading = tabHost
          .newTabSpec("Reading")
          .setIndicator("", ressources.getDrawable(R.drawable.reading))
          .setContent(intentReading);

Intent intentMedication = new Intent().setClass(this, Medication.class);
        TabSpec tabMedication = tabHost
          .newTabSpec("Medication")
          .setIndicator("", ressources.getDrawable(R.drawable.medication))
          .setContent(intentMedication);

tabHost.addTab(tabReading);
tabHost.addTab(tabMedication);

答案 1 :(得分:0)

请在下面的代码中写下代码而不是代码,以便在一个TabActivity中添加多个活动,它将解决您的问题。

ActivityStack.java

private Stack<String> stack;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (stack == null)
        stack = new Stack<String>();
    // start default activity
    push("FirstStackActivity", new Intent(this, Tab_SampleActivity.class));
}

@Override
public void finishFromChild(Activity child) {
    pop();
}

@Override
public void onBackPressed() {
    pop();
}

public void push(String id, Intent intent) {
    Window window = getLocalActivityManager().startActivity(id, intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
    if (window != null) {
        stack.push(id);
        setContentView(window.getDecorView());
    }
}

public void pop() {
    if (stack.size() == 1)
        finish();
    LocalActivityManager manager = getLocalActivityManager();
    manager.destroyActivity(stack.pop(), true);
    if (stack.size() > 0) {
        Intent lastIntent = manager.getActivity(stack.peek()).getIntent();
        Window newWindow = manager.startActivity(stack.peek(), lastIntent);
        setContentView(newWindow.getDecorView());
    }
}

TabActivity.java

public class TabActivity extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_screen);
        TabHost tabHost = getTabHost();
        Intent intent = new Intent().setClass(this, ActivityStack.class);
        TabHost.TabSpec spec = tabHost.newTabSpec("tabId").setIndicator("Temp", getResources().getDrawable(R.drawable.home));
        spec.setContent(intent);

        tabHost.addTab(spec);

        Intent intent1 = new Intent().setClass(this, ActivityStack.class);
        TabHost.TabSpec spec1 = tabHost.newTabSpec("tabId").setIndicator("Temp", getResources().getDrawable(R.drawable.invoice));
        spec1.setContent(intent1);
        tabHost.addTab(spec1);

        tabHost.setCurrentTab(0);
    }
}

FirstActivity.java

public class FirstActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textView = new TextView(this);
        textView.setText("Tab Sample Activity ");
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getParent(), SecondActivity.class);
                ActivityStack activityStack = (ActivityStack) getParent();
                activityStack.push("SecondActivity", intent);
            }
        });
        setContentView(textView);
    }
}

SecondActivity.java

public class SecondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView textView = new TextView(this);
        textView.setText("First Stack Activity ");
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(getParent(), ThirdActivity.class);
                ActivityStack activityStack = (ActivityStack) getParent();
                activityStack.push("ThirdActivity", intent);
            }
        });
        setContentView(textView);
    }
}

ThirdActivity.java

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

将以下XML文件添加到res / layout文件夹中。

1)tab_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<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" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:padding="3dp" >

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_above="@android:id/tabs"
            android:layout_weight="1" />

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>

</TabHost>

2)main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />

</LinearLayout>

的AndroidManifest.xml: -

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.tabsample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".FirstActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".TabActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ActivityStack"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".ThirdActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

有关在一个TabActivity下添加多个活动的详细信息,请参阅以下链接。

Android - Multiple Android Activities under one TabActivity