마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

[수정] 버튼을 클릭시, 탭의 내용 부분으로 가게 하고 싶습니다.

0 추천

 

안녕하세요.

제가 어제 이 글을 올렸었는데, 글의 내용이 너무 이해하기 힘드셨을수도 있을것 같아서,

모르는 부분을 다시 정리해서 글을 올려봅니다.  아래의 이미지를 보시면, 아래의 간단한 이미지와

그 아래에 버튼 부분이 있습니다. 화면에서 조금 짤렸는데, 저 부분이 버튼인데 저 버튼을 눌렀을

경우에, 그림에서 보이는 TAB2 번째로 이동시키게 하려고 하는데.. 이 부분때문에 계속 찾아도 보았고

질문을 올려보았지만.. 그 해답을 찾지 못해서, 다시금이나마 질문을 올립니다.

아래의 이미지에 대한 자바소스와 XML 소스를 아래에 더해보았습니다.

제가 갈피를 못잡고 있는 부분에 대해서 아래의 자바부분에 주석처리를 한번 해 보았습니다.

저 부분에서, TAB2로 이동시키는 코드만 작성하면 될것 같은데 그 부분을 못하고 있네요 ㅠ

도움 부탁드립니다.

8000자가 넘는 부분은 아래의 댓글 부분에 좀 더 달아보겠습니다.

 

다단.PNG

 

< XML 코드 부분입니다 >

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_date_and__seat"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="ex.real_project.Date_and_Seat">

    <RelativeLayout
        android:id="@+id/Date_and_Seat_Toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/Before_Date_and_Seat_Image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="3dp"
        android:layout_marginLeft="3dp"
        android:background="@drawable/before_image" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="예매"
        android:textSize="30dp"
        android:textStyle="bold"
        android:layout_marginTop="10dp"
        android:layout_centerHorizontal="true" />

    </RelativeLayout>

            
공산당 (1,960 포인트) 님이 2016년 11월 3일 질문
<TabHost
            android:id="@+id/tabhost"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

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

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

                    </TabWidget>

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

                    <ScrollView
                        android:id="@+id/tab1"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent" >

                        <RelativeLayout
                            android:id="@+id/tableRow1"
                            android:layout_width="fill_parent"
                            android:layout_height="fill_parent">


                            <ImageView
                                android:id="@+id/imageView"
                                android:layout_width="match_parent"
                                android:background="@drawable/first_tab_image"
                                android:layout_height="350dp"
                                />

                            <Button
                                android:id="@+id/First_Tab_Button"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@id/imageView"
                                android:layout_marginTop="20dp"
                                android:text="선택"
                                android:layout_centerHorizontal="true"
                                android:textAlignment="center"
                                android:textColor="#9000ff"
                                android:background="@drawable/white_round_button"
                                />


                        </RelativeLayout>

                    </ScrollView>

                    <ScrollView
                        android:id="@+id/tab2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >

                        <Button
                                android:id="@+id/button2"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:text="test2"
                                android:textAppearance="?android:attr/textAppearanceMedium" />

                    </ScrollView>

                    <ScrollView
                        android:id="@+id/tab3"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent" >

                        <TextView
                            android:id="@+id/textView3"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="test3"
                            android:textAppearance="?android:attr/textAppearanceMedium" />
                    </ScrollView>

                </FrameLayout>
            </LinearLayout>
    </TabHost>

</LinearLayout>


< 아래는 자바 코드 입니다 >

package ex.real_project;

import android.content.Intent;
import android.graphics.drawable.Icon;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.Toast;

public class Date_and_Seat extends AppCompatActivity {

    ImageView Before_Date_and_Seat;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_date_and__seat);


                TabHost tab_host = (TabHost) findViewById(R.id.tabhost);
                tab_host.setup();

                TabHost.TabSpec Tap1 = tab_host.newTabSpec("tab1");
                Tap1.setIndicator("일번");
                Tap1.setContent(R.id.tab1);
                tab_host.addTab(Tap1);

                final TabHost.TabSpec Tap2 = tab_host.newTabSpec("tab2");
                Tap2.setIndicator("이번");
                Tap2.setContent(R.id.tab2);
                tab_host.addTab(Tap2);

                TabHost.TabSpec Tap3 = tab_host.newTabSpec("tab2");
                Tap3.setIndicator("삼번");
                Tap3.setContent(R.id.tab3);
                tab_host.addTab(Tap3);

                tab_host.setCurrentTab(0);


        /*
        btn = (Button) findViewById(R.id.First_Tab_Button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Tap2.setIndicator("선택");
            }
        });
        */            // 여기서 클릭시에, Tab2 부분으로 이동시키고 싶은데 그 방법을, 잘 모르겠어요..

      
    }
}

1개의 답변

0 추천
 
채택된 답변
TabHost는 모르겠으나

방금 디자인 서포트라이브러리의 TabLayout으로 확인 해보니

동적으로 탭을 변경 할수 있네요.

 

구조적으로 TabHost의 동적 변환이 어려운 부분이시면 TabLayout으로 해보시는게 어떠실지??
Development Guy (70,570 포인트) 님이 2016년 11월 3일 답변
공산당님이 2016년 11월 18일 채택됨
헉.. 알겠습니다. 만든 탭 호스트의 부분을 탭 레이아웃으로 다시 변환해 보아야겠어요 ㅠ 정말 감사합니다.
앗 개발꾼님, tab_host 내의 setCurrentTab를 사용해서, 해결 했습니다.
감사합니다 ㅠㅠ
대부분ㅇ의 사람들이 제가 지금 만들고 있는 탭 호스트를 탭레이아웃으로 바꿔보는건 어떻게냐고들 말씀을 많이 하시는데, 이유가 뭔가요?
탭 호스트는 사람들이 많이 사용하지 않아서인가요? 아니면 탭 레이아웃이 더 보편적인 방법이라서 그런건가요?
제가 TabLayout으로 확인 해보라고 말씀드린 이유는 그냥 단순히 TabHost를 많이 사용해 보질 않아서 입니다.

그냥 TabLayout이 더 익숙해서 동적으로 탭 변경이 되는지 확인해보고 답변 드린것이고 다른분들이 바꿔보길 추천하신 이유도 단순히 TabHost보다는 최근에 나온 위젯인 이유이지 아닐까 싶습니다.

TabLayout은 구글에서 디자인 서포트 라이브러리를 내놓을때 그 안에 포함된 위젯으로 Toolbar와 함께 조금 더 유기적인 동작을 할 수 있는 화면 구성을 할 수 있도록 지원해 주는거 같습니다.

어떤것을 쓰든지 정답은 없어요 그냥 상황에 맞게 적절하게 사용하시면 될거 같습니다.
답변 정말 감사드립니다.
...