在我的 XML 中,我有一个带有切换按钮和 2 组单选按钮的相对布局。
现在的设置是,当选择第一个单选按钮组中的单选按钮“spells”时,它应该将单选按钮组“spellButtons”带到前面。
但是,只有当我至少触摸过一次切换按钮时,这才有效。理想情况下,我不必触摸切换按钮即可使 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
}
});
最佳答案
在你的 onClickListener 中检查你的 RadioGroup 拼写按钮是否 isSelected()。 如果您希望在单击拼写 RadioButton 时显示它,则不需要 if 循环。为什么不使用 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");
}
});
如果您想在单击另一个单选按钮时隐藏它,只需 setOnClickListener 即可,并使拼写按钮可见性不可见或消失
关于java - 单击监听器上的单选按钮仅在选择切换按钮后才起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20023454/