클릭 수신기의 라디오 버튼은 토글 버튼을 선택한 후에 만 작동합니다.

StackOverflow https://stackoverflow.com//questions/20023454

문제

내 XML에서는 토글 버튼과 2 개의 라디오 버튼이있는 상대 레이아웃이 있습니다.

첫 번째 라디오 버튼 그룹의 라디오 버튼 "주문"이 선택되면 라디오 버튼 그룹이 "SpellButtons"를 앞쪽까지 가져와야합니다.

그러나 적어도 한 번 토글 버튼을 눌렀다면 작동합니다.이상적으로, 특히 Toggle 버튼을 결국 일시 중지 버튼을 만들려고하는 것이 깜박이는 코드를 onclick 코드로 만족할 필요가 없습니다.

여기 내 XML :

    RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/mainLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
    tools:context=".MainActivity"
     >

    <ToggleButton
        android:id="@+id/pause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="Toggle Yo" />

    <RadioGroup
        android:id="@+id/combatSwitchButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/spells"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:background="@drawable/radio"
            android:button="@null"
            android:textColor="@android:color/black" />

        <RadioButton
            android:id="@+id/summons"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:background="@drawable/radio"
            android:button="@null"
            android:textColor="@android:color/black" />

        <RadioButton
            android:id="@+id/power"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="false"
            android:background="@drawable/radio"
            android:button="@null"
            android:textColor="@android:color/black" />
    </RadioGroup>

    <RadioGroup
        android:id="@+id/spellButtons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/spell1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="hi" 
            android:textColor="@android:color/black"/>

        <RadioButton
            android:id="@+id/spell2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" " />

        <RadioButton
            android:id="@+id/spell3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" " />
        <RadioButton
            android:id="@+id/spell4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" " />

        <RadioButton
            android:id="@+id/drone1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" " />

        <RadioButton
            android:id="@+id/drone2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" " />
    </RadioGroup>

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/combatSwitchButtons"
        android:layout_centerVertical="true" >

    </RadioGroup>

</RelativeLayout>
.

여기에 내 코드가 있습니다

        findViewById(R.id.combatSwitchButtons).bringToFront();
    findViewById(R.id.pause).bringToFront();

    //this sets up the toggle button which will become a pause button
    findViewById(R.id.pause).setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {               
            //TODO: Pause the game
        }
    });

    //This is for if we click spells on the switch interface. It'll make the spell buttons appear
    findViewById(R.id.spells).setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {   
            //change the mode
            game.spells=false;
            game.power=false;
            game.summons=true;

            //add the spells buttons and the drones
            if(findViewById(R.id.spellButtons).isSelected())
                            {
                findViewById(R.id.spellButtons).bringToFront();
                                    System.out.println("brought it to the front");
                            }
            else{
                findViewById(R.id.spellButtons).setVisibility(View.INVISIBLE);
            }   

            //TODO: remove other buttons
        }
    });
.

도움이 되었습니까?

해결책 2

분명히 Spellbutton 라디오 버튼 그룹을 앞쪽으로 올리고 OnClick Listener에 입력하기 전에 보이지 않게 설정해야했습니다.OnClick Listener에서 SetVisibility (View.Visible)를 사용할 수 있습니다.

내 코드가

처럼 보이게되었습니다.
    findViewById(R.id.combatSwitchButtons).bringToFront();
    findViewById(R.id.pause).bringToFront();
    findViewById(R.id.spellButtons).bringToFront(); 
    findViewById(R.id.spellButtons).setVisibility(View.GONE);

    //this sets up what each button does.
    findViewById(R.id.pause).setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {               
            //TODO: Pause the game
        }
    });

    /*ALLL the on click listeners*/
    //This is for if we click spells on the switch interface. It'll make the spell buttons appear
    findViewById(R.id.spells).setOnClickListener(new OnClickListener()
    {
        public void onClick(View v)
        {   
            //change the mode
            game.spells=false;
            game.power=false;
            game.summons=true;

            //add the spells buttons and the drones
                findViewById(R.id.spellButtons).setVisibility(View.VISIBLE);    
    //TODO: remove other buttons
        }
    });
.

다른 팁

onClickListener의 RadioGroup SpellButtons가 isselected () 인 경우 검사를 위해 확인하십시오. 주문 RadioButton을 클릭 할 때 표시되기를 원한다면 루프가 필요하지 않습니다.BringToFront를 사용하는 대신 SetVisibility ()를 사용하지 않는 이유는 무엇입니까?

다음과 같은 것 :

findViewById(R.id.spells).setOnClickListener(new OnClickListener()
{
    public void onClick(View v)
    {   
        //change the mode
        game.spells=false;
        game.power=false;
        game.summons=true;

        //add the spells buttons and the drones
        ((RadioGroup) findViewById(R.id.spellButtons)).setVisibility(View.VISIBLE);
        System.out.println("brought it to the front");
    }
});
.

및 다른 RadioButton을 클릭하면 SetOnClickListener를 클릭하고 SpellButtons 가시성을 보이지 않거나 사라지거나 사라지게하십시오

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top