0

我需要帮助来解决这个问题。我的编码仅适用于第一个布局但不适用于第二个布局的问题。

这是我的标签 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">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <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"/>
    </LinearLayout>
</TabHost>

这是我的标签 java 文件

package watresystem.com;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;


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

        TabHost tabHost = getTabHost();

        // Tab for Output
        TabSpec outputspec = tabHost.newTabSpec("Output");
        // setting Title and Icon for the Tab
        outputspec.setIndicator("Output", getResources().getDrawable(R.drawable.icon_output_tab));
        Intent outputIntent = new Intent(this, OutputActivity.class);
        outputspec.setContent(outputIntent);

        // Tab for Input
        TabSpec inputspec = tabHost.newTabSpec("Input");
        inputspec.setIndicator("Input", getResources().getDrawable(R.drawable.icon_input_tab));
        Intent inputIntent = new Intent(this, InputActivity.class);
        inputspec.setContent(inputIntent);

        // Tab for Time & Date
        TabSpec timespec = tabHost.newTabSpec("Time");
        timespec.setIndicator("Time", getResources().getDrawable(R.drawable.icon_time_tab));
        Intent timeIntent = new Intent(this, TimeActivity.class);
        timespec.setContent(timeIntent);

        // Adding all TabSpec to TabHost
        tabHost.addTab(outputspec); // Adding output tab
        tabHost.addTab(inputspec); // Adding input tab
        tabHost.addTab(timespec); // Adding time tab
    }
}

我的标签布局上输入按钮的java代码

package watresystem.com;

import android.app.Activity;
import android.os.Bundle;
import ioio.lib.api.DigitalInput;
import ioio.lib.api.exception.ConnectionLostException;
import ioio.lib.util.AbstractIOIOActivity;
import android.widget.TextView;

public class InputActivity extends AbstractIOIOActivity {
    private final int BUTTON1_PIN = 37;
    private final int BUTTON2_PIN = 32;
    private final int BUTTON3_PIN = 33;

    private TextView mBtn1TextView;
    private TextView mBtn2TextView;
    private TextView mBtn3TextView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.input_layout);

        mBtn1TextView = (TextView) findViewById(R.id.btn1TextView);
        mBtn2TextView = (TextView) findViewById(R.id.btn2TextView);
        mBtn3TextView = (TextView) findViewById(R.id.btn3TextView);
    }

    class IOIOThread extends AbstractIOIOActivity.IOIOThread {
        private DigitalInput mButton1;
        private DigitalInput mButton2;
        private DigitalInput mButton3;

        @Override
        public void setup() throws ConnectionLostException {
            try {
                mButton1 = ioio_.openDigitalInput(BUTTON1_PIN, DigitalInput.Spec.Mode.PULL_UP);
                mButton2 = ioio_.openDigitalInput(BUTTON2_PIN, DigitalInput.Spec.Mode.PULL_UP);
                mButton3 = ioio_.openDigitalInput(BUTTON3_PIN, DigitalInput.Spec.Mode.PULL_UP);
            } catch (ConnectionLostException e) {
                throw e;
            }
        }

        @Override
        public void loop() throws ConnectionLostException {
            try {
                String button1txt;
                String button2txt;
                String button3txt;
                final boolean reading1 = mButton1.read();
                final boolean reading2 = mButton2.read();
                final boolean reading3 = mButton3.read();

                if (!reading1) {
                    button1txt = getString(R.string.button1) + " active!";
                } else {
                    button1txt = getString(R.string.button1);
                }
                if (!reading2) {
                    button2txt = getString(R.string.button2) + " active!";
                } else {
                    button2txt = getString(R.string.button2);
                }
                if (!reading3) {
                    button3txt = getString(R.string.button3) + " active!";
                } else {
                    button3txt = getString(R.string.button3);
                }

                setText(button1txt, button2txt, button3txt);
                sleep(10);
            } catch (InterruptedException e) {
                ioio_.disconnect();
            } catch (ConnectionLostException e) {
                throw e;
            }
        }
    }

    @Override
    protected AbstractIOIOActivity.IOIOThread createIOIOThread() {
        return new IOIOThread();
    }

    private void setText(final String str1, final String str2, final String str3) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mBtn1TextView.setText(str1);
                mBtn2TextView.setText(str2);
                mBtn3TextView.setText(str3);
            }
        });
    }
}

我的输入按钮的 xml 布局

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

    <!-- Screen Design for Input -->

    <TextView
        android:id="@+id/btn1TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button1"
        android:textSize="30sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/btn2TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button2"
        android:textSize="30sp"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/btn3TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button3"
        android:textSize="30sp"
        android:textStyle="bold" />


</LinearLayout>
4

1 回答 1

0

看到这是活动

public class MainActivity extends TabActivity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources ressources = getResources(); 
    TabHost tabHost = getTabHost(); 

    // Android tab
    Intent intentAndroid = new Intent().setClass(this, AndroidActivity.class);
    TabSpec tabSpecAndroid = tabHost
      .newTabSpec("Android")
      .setIndicator("", ressources.getDrawable(R.drawable.icon_android_config))
      .setContent(intentAndroid);

    // Apple tab
    Intent intentApple = new Intent().setClass(this, AppleActivity.class);
    TabSpec tabSpecApple = tabHost
      .newTabSpec("Apple")
      .setIndicator("", ressources.getDrawable(R.drawable.icon_apple_config))
      .setContent(intentApple);

    // Windows tab
    Intent intentWindows = new Intent().setClass(this, WindowsActivity.class);
    TabSpec tabSpecWindows = tabHost
      .newTabSpec("Windows")
      .setIndicator("", ressources.getDrawable(R.drawable.icon_windows_config))
      .setContent(intentWindows);

    // Blackberry tab
    Intent intentBerry = new Intent().setClass(this, BlackBerryActivity.class);
    TabSpec tabSpecBerry = tabHost
      .newTabSpec("Berry")
      .setIndicator("", ressources.getDrawable(R.drawable.icon_blackberry_config))
      .setContent(intentBerry);

    // add all tabs 
    tabHost.addTab(tabSpecAndroid);
    tabHost.addTab(tabSpecApple);
    tabHost.addTab(tabSpecWindows);
    tabHost.addTab(tabSpecBerry);

    //set Windows tab as default (zero based)
    tabHost.setCurrentTab(2);
} 
 }

这是针对 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">
 <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>

看到这个http://www.mkyong.com/android/android-tablayout-example/

于 2012-07-31T09:08:04.840 回答