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

탭의 버튼 이벤트 관련해서 setText를 해주고싶은데.. 방법을 모르겠어요.

0 추천

 

안녕하세요.

아래의 사진은 탭호스트로 구현을 해 놓은 화면입니다.

다름이 아니라, tab2를 클릭하면, 아래에 버튼이 하나 나오는데, 그 버튼을 눌렀을때 tab2의 이름이

다른걸로 바뀌었으면 좋겠습니다. 그런데, 그 방법을 잘 몰라서 글을 올렸습니다.

어떤 분꼐서, tabHost.setCurrentTabByTag(); 를 사용하면 된다고 하셨는데 tabHost를 사용해서

만들어 보았지만.. 어떻게 해야할지 잘 모르겠어요..

그래서 TAB2 부분이 아이디가 tab2로 주어서, tab2로 해보려고 했는데도 잘 되질 않네요..

 

어떻게 해야할까요 .. ㅠ

 

package ex.real_project;

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);

                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.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tab_host.setCurrentTabByTag();
            }
        });


        Before_Date_and_Seat = (ImageView) findViewById(R.id.Before_Date_and_Seat_Image);
        Before_Date_and_Seat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        int position = getIntent().getExtras().getInt("poss");
        Toast.makeText(getApplicationContext(),"으악" + position,Toast.LENGTH_SHORT).show();
    }
}
Kind카인드 (3,600 포인트) 님이 2016년 11월 1일 질문

1개의 답변

0 추천
익명사용자 님이 2016년 11월 2일 답변
답변 감사드립니다..
말씀해주신 사이트를 보고, 제 코드에 적용을 시켜보려고 하는데.. 자꾸 버벅이네요 ㅠ 제가 만든 코드를 한번만 보아주실수 있을까요..
아래에서, 버튼(btn)을 눌렀을 경우에 Tap1의 이름이 바뀌어야 할텐데..
어떻게 바꾸어주어야 할지 ㅠ ㅠ


package ex.real_project;

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();

                final TabHost.TabSpec Tap1 = tab_host.newTabSpec("tab1");
                Tap1.setIndicator("영화");
                Tap1.setContent(R.id.tab1);
                tab_host.addTab(Tap1);

                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) {
                Tap1.setIndicator("우닥");   // 이 부분에서 변경이 필요한건지....
            }
        });



        Before_Date_and_Seat = (ImageView) findViewById(R.id.Before_Date_and_Seat_Image);
        Before_Date_and_Seat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        int position = getIntent().getExtras().getInt("poss");
        Toast.makeText(getApplicationContext(),"으악" + position,Toast.LENGTH_SHORT).show();
    }
}
btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
((TextView) tabHost.getTabWidget().getChildAt(0)
            .findViewById(android.R.id.title)).setText("This sucks, I know.");
            }
        });

getChildAt(0) <--이부부만 원하는 탭으로 변경하시면됩니다
ex) 1번탭 getChildAt(0)
2번탭 getChildAt(1)
3번탭 getChildAt(2)
...