Pulsante di road on On Click Listener funziona solo dopo aver selezionato il pulsante di commutazione

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

Domanda

Nel mio XML, ho un layout relativo con un pulsante di commutazione e 2 gruppi di pulsanti radio.

Proprio ora è impostato in modo che quando è selezionato il pulsante di opzione "incantesimi" nel primo gruppo di pulsanti di opzione, dovrebbe portare il gruppo del tasto radio "Spellbuttons" fino alla parte anteriore.

Tuttavia, che funziona solo se ho toccato il pulsante Attiva / disattiva almeno una volta.Idealmente, non dovrei toccare il pulsante Toggle per il mio codice onClick per funzionare, soprattutto perché ho intenzione di effettuare il pulsante a levetta un pulsante di pausa.

Ecco il mio 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>
.

Ed Ecco il mio codice

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

È stato utile?

Soluzione 2

Apparentemente avevo bisogno di portare il gruppo di pulsanti radiocomandi di Spellbutton fino alla parte anteriore e impostarlo in invisibile prima di inserire il listener onclick.Nell'ascoltatore OnClick, potrei usare la ritaglificazione (view.visible).

Il mio codice è finito per sembrare

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

Altri suggerimenti

Nella tua onclickListener per incantesimi il tuo controllo se il tuo radiogruppo SpellButtons è issato (). Se vuoi che si presentasse quando fai clic sugli incantesimi RadioButton non ti serve se loop.Invece di usare brindtofront perché non usi Sevistibilità ()?

Qualcosa del genere:

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");
    }
});
.

E se vuoi nasconderlo quando si fa clic su un altro RadioButton solo SetonClickListener per questo e fai la visibilità Spellbuttons invisibile o andata

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top