Переключатель на Click Listerer работает только после выбора кнопки Toggle

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

Вопрос

В моем XML у меня есть относительную макет с помощью кнопки переключения и 2 группами радиопередач.

Сейчас он установлен так, что когда выбрана кнопка «Заклинания» в первой группе радиопередачи, она должна принести группу «Заведующие раза» рацион «Спечки».

Однако, которые работает только, если я коснулся кнопки тумана хотя бы один раз.В идеале я не должен прикоснуться к кнопке Toggle для моего onClick Code для работы, тем более что я собираюсь сделать кнопку переключения кнопки паузы в конце концов.

Вот мой 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

, по-видимому, мне нужно было донести группу кнопок Weellbutton Radio вплоть до спереди и установить ее на невидимую, прежде чем я когда-либо входил в прослушиватель OnClick.В прослушивателе OnClick я мог бы использовать STVisitive (View.isible).

Мой код закончился, выглядел как

    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 для заклинаний вашей проверки, если ваша радиогруппа введена в Importttons (). Если вы хотите, чтобы это было отображено, когда вы нажимаете на заклинания Radiobutton, вам не нужно так, если петля.Вместо того, чтобы использовать Breauttofront, почему вы не используете 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