[Android studio] tabhost solution for multiple controls in a single tab

Posted by eruna on Fri, 27 Mar 2020 16:54:17 +0100

Preface:

Received the teacher's task a few days ago, need to do an android APP, mainly with the temperature and humidity sensor for tcp communication.
TabHost is used in the design process. If TabHost holds multiple controls in a tab, these controls will be displayed on each tab.
The author uses this article to record the solution process.

Email: looker@outlook.com

Text:

The following is the interface design adopted by the author:

To change and store the mailing address in tab3:

The following is the interface after installation:

It can be found that the original port number and text box in tab3 will be displayed in tab1 and tab2, which is not the effect we want.

The author uses the following methods to fail (TAT):

First,

Two.

final Context tabContext = MainActivity.this;
TabSpec ts1 = tabHost.newTabSpec("tab3");
    ts1.setIndicator("Set up");
    ts1.setContent(new TabHost.TabContentFactory() { // Create group
        public View createTabContent(String tag) {
            LinearLayout tab3Group = new LinearLayout(tabContext);
            tab3Group.setLayoutParams(
                new LayoutParams(
                    LayoutParams.MATCH_PARENT,
                    LayoutParams.WRAP_CONTENT));
            tab3Group.setOrientation(LinearLayout.VERTICAL);

            TextView ipname = new TextView(tabContext);
            ipname.setText("IP Address:");
            tab3Group.addView(ipname);

But he was prepared to hide him directly:

setVisibility(View.INVISIBLE); // View is not visible, but still occupies the size and position when it is visible
setVisibility(View.GONE);      // View is not visible and does not occupy space
Step 1: Set initial tab interface
    tabs.setCurrentTab(0); // Set the first Tab currently displayed, which can be omitted
Step 2: Set control not to display in initial tab3
    ipname.setVisibility(View.INVISIBLE);
    ip.setVisibility(View.INVISIBLE);
    portname.setVisibility(View.INVISIBLE);
    port.setVisibility(View.INVISIBLE);
Step 2: Create a new listening event. If it is tab3, the control will be displayed
    // onCreate:
    tabs = (TabHost) findViewById(R.id.tabhost);
    tabs.setup();
    tabs.addTab(tabs.newTabSpec("tab1")
            .setIndicator("data")
            .setContent(R.id.rcv));
    tabs.addTab(tabs.newTabSpec("tab2")
            .setIndicator("TAB_2")
            .setContent(R.id.line2));
    tabs.addTab(tabs.newTabSpec("tab3")
            .setIndicator("Set up")
            .setContent(R.id.portname));
    tabs.setOnTabChangedListener(new MyTabChangedListener()); // Create monitoring

    // Listening and processing function
    private class MyTabChangedListener implements TabHost.OnTabChangeListener {
        @Override
        public void onTabChanged(String tabId) {
            if(tabId.equals("tab1") || tabId.equals("tab2")) {
                ipname.setVisibility(View.INVISIBLE);
                ip.setVisibility(View.INVISIBLE);
                portname.setVisibility(View.INVISIBLE);
                port.setVisibility(View.INVISIBLE);
            }
            else {
                ipname.setVisibility(View.VISIBLE);
                ip.setVisibility(View.VISIBLE);
                portname.setVisibility(View.VISIBLE);
                port.setVisibility(View.VISIBLE);
            }
        }

be accomplished!


The following provides the download address of java files of this project:

Links: https://pan.baidu.com/s/1F8MhRM5dyoikfB8bwj1M4A
Password: x1n1

If you need a complete project, please contact my email address: looker@outlook.com

Topics: Android Java