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

앱실행시 눌려진 라디오버튼의 fragment 불러오기

0 추천

카카오톡의 메뉴와 레이아웃과 같은 형식으로 제작하려고 합니다

3개의 라디오버튼과 3개의 액티비티가 있습니다 

라디오 버튼중 하나는 checked  = true 로 주었구요 


    RadioButton rb_human, rb_msg, rb_other;

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

        rb_human = (RadioButton)findViewById(R.id.rb_human);
        rb_msg = (RadioButton)findViewById(R.id.rb_msg);
        rb_other = (RadioButton)findViewById(R.id.rb_other);

        rb_human.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                Sub_1 sub_1 = new Sub_1();
                transaction.replace(R.id.layout_main, sub_1);
                transaction.commit();


            }
        });


        rb_msg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                Sub_2 sub_2 = new Sub_2();
                transaction.replace(R.id.layout_main, sub_2);
                transaction.commit();


            }
        });

        rb_other.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
                Sub_3 sub_3 = new Sub_3();
                transaction.replace(R.id.layout_main, sub_3);
                transaction.commit();


            }
        });

이게 메인 액티비티의 java 코드입니다  앱 실행하였을때 체크되어있는 버튼의 fragment 를 불러오고싶습니다 


<RadioGroup
    android:id="@+id/rb_mainmenu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginLeft="5dp"
    android:layout_marginTop="30dp"
    android:layout_weight="1">

    <RadioButton
        android:id="@+id/rb_human"
        android:layout_width="50dp"
        android:layout_height="40dp"
        android:width="50dp"
        android:height="50dp"
        android:background="@drawable/imgtoggle_human"
        android:button="@null"
        android:checked="true" />

    <RadioButton
        android:id="@+id/rb_msg"
        android:layout_width="50dp"
        android:layout_height="40dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:background="@drawable/imgtoggle_message"
        android:button="@null"
        android:scaleType="fitXY" />

    <RadioButton
        android:id="@+id/rb_other"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginLeft="3dp"
        android:background="@drawable/imgtoggle_other"
        android:button="@null"
        android:scaleType="fitXY" />

</RadioGroup>

이건 라디오 그룹의 xml 코드이구요 메인 xml 의 화면에 체크되어서 불러올 화면을 메인화면에 미리 넣어두어야 하는건가요? 아니면 체크되어있는 라디오버튼의 fragment 를 앱 실행시 불러올수있는 방법이 있을까요 ?

kyaha (260 포인트) 님이 2020년 2월 27일 질문

1개의 답변

0 추천
isChecked 호출 시 true인 레이아웃을 띄우시면 될 듯 합니다.

뭐 xml에 선언된 값이 rb_human 만  true라 무조건 rb_human 쪽이 나올 듯 은 하지만요.
익명사용자 님이 2020년 3월 2일 답변
...