单选组 Android 保持默认
Radio Group Android stays on default
我在 Google 和 Whosebug 上搜索任何解决方案,我发现了很多,但其中 none 对我有用。
我有 3 个 RadioButtons,因为它应该只有一个 selectable。
现在我想知道当我按下 "Visit" 按钮时 select 编辑了哪个单选按钮。
我的 VisitButton 函数被调用了,但是当我尝试读取 RadioButton 时,结果总是一样的。
不管我选择哪个select,它总是选择默认按钮。
我尝试了各种不同的方法,onChangeCheckListener、onClickListener,但其中 none 似乎有效。
我做错了什么?
这是我的代码片段:
MicrolabB = (RadioButton) findViewById(R.id.Microlab);
MoodleB = (RadioButton) findViewById(R.id.Moodle);
ISAB = (RadioButton) findViewById(R.id.ISA);
PortalSelect = (RadioGroup) findViewById(R.id.PortalSelect);
VISIT = (Button) findViewById(R.id.Visit);
MicrolabB.setOnClickListener(this);
ISAB.setOnClickListener(this);
MoodleB.setOnClickListener(this);
public void VisitWebPage(View v){
if(v.getId()==R.id.Visit){
if(MicrolabB.isChecked())
ApduNFC.Portal =1;
if(ISAB.isChecked())
ApduNFC.Portal=2;
if(MoodleB.isChecked())
ApduNFC.Portal=3;
Intent intent = new Intent(Start.this,ApduNFC.class);
startActivity(intent);
}
}
还有我的XML:
<RadioGroup
android:id="@+id/PortalSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true" >
<RadioButton
android:id="@+id/Microlab"
android:layout_width="133dp"
android:layout_height="wrap_content"
android:text="Microlab"
android:clickable="true"
/>
<RadioButton
android:id="@+id/ISA"
android:layout_width="126dp"
android:layout_height="wrap_content"
android:text="IS-A"
android:clickable="true"
android:checked="true" />
<RadioButton
android:id="@+id/Moodle"
android:layout_width="116dp"
android:layout_height="wrap_content"
android:text="Moodle"
android:clickable="true" />
</RadioGroup>
<Button
android:id="@+id/Visit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:onClick="VisitWebPage"
android:text="Visit" />
首先,在此示例中,您肯定不需要 RadioButtons 上的侦听器。
获得选中按钮的最佳选项来自PortalSelect.getCheckedRadioButtonId().
您将获得选中的 RadioButton 的 ID。
别忘了检查id是否存在;默认情况下,RadioGroup 初始化为选中 none 个 RadioButtons;
参考Google RadioGroup网站:
http://developer.android.com/reference/android/widget/RadioGroup.html
在你的VisitWebPage
中先添加PortalSelect.getCheckedRadioButtonId()
赞
public void VisitWebPage(View v){
if(v.getId()==R.id.Visit){
int selectedId = PortalSelect.getCheckedRadioButtonId();
if(selectedId == MicrolabB.getId())
Log.d("RB Selected","1");
if(selectedId ==ISAB.getId())
Log.d("RB Selected","2");
if(selectedId ==MoodleB.getId())
Log.d("RB Selected","3");
//Intent intent = new Intent(Start.this,ApduNFC.class);
//startActivity(intent);
}
}
解决了问题。
public void VisitWebPage(View v){
final RadioGroup PortalSelect = (RadioGroup) findViewById(R.id.PortalSelect);
int ID = PortalSelect.getCheckedRadioButtonId();
if(v.getId()==R.id.Visit){
int rb1Id = MicrolabB.getId();
if(ID == MicrolabB.getId())
Log.d("RB Selected","1");
if(ID == ISAB.getId())
Log.d("RB Selected","2");
if(ID == MoodleB.getId())
Log.d("RB Selected","3");
Intent intent = new Intent(Start.this,ApduNFC.class);
startActivity(intent);
}
}
我输入行后
final RadioGroup PortalSelect = (RadioGroup) findViewById(R.id.PortalSelect);
在 VisitWebPage 函数中似乎一切正常。我不知道为什么,因为我之前在这个 class 中定义了 Radio Group。但我很高兴它现在可以正常工作了。
我在 Google 和 Whosebug 上搜索任何解决方案,我发现了很多,但其中 none 对我有用。
我有 3 个 RadioButtons,因为它应该只有一个 selectable。
现在我想知道当我按下 "Visit" 按钮时 select 编辑了哪个单选按钮。 我的 VisitButton 函数被调用了,但是当我尝试读取 RadioButton 时,结果总是一样的。
不管我选择哪个select,它总是选择默认按钮。
我尝试了各种不同的方法,onChangeCheckListener、onClickListener,但其中 none 似乎有效。
我做错了什么?
这是我的代码片段:
MicrolabB = (RadioButton) findViewById(R.id.Microlab);
MoodleB = (RadioButton) findViewById(R.id.Moodle);
ISAB = (RadioButton) findViewById(R.id.ISA);
PortalSelect = (RadioGroup) findViewById(R.id.PortalSelect);
VISIT = (Button) findViewById(R.id.Visit);
MicrolabB.setOnClickListener(this);
ISAB.setOnClickListener(this);
MoodleB.setOnClickListener(this);
public void VisitWebPage(View v){
if(v.getId()==R.id.Visit){
if(MicrolabB.isChecked())
ApduNFC.Portal =1;
if(ISAB.isChecked())
ApduNFC.Portal=2;
if(MoodleB.isChecked())
ApduNFC.Portal=3;
Intent intent = new Intent(Start.this,ApduNFC.class);
startActivity(intent);
}
}
还有我的XML:
<RadioGroup
android:id="@+id/PortalSelect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true" >
<RadioButton
android:id="@+id/Microlab"
android:layout_width="133dp"
android:layout_height="wrap_content"
android:text="Microlab"
android:clickable="true"
/>
<RadioButton
android:id="@+id/ISA"
android:layout_width="126dp"
android:layout_height="wrap_content"
android:text="IS-A"
android:clickable="true"
android:checked="true" />
<RadioButton
android:id="@+id/Moodle"
android:layout_width="116dp"
android:layout_height="wrap_content"
android:text="Moodle"
android:clickable="true" />
</RadioGroup>
<Button
android:id="@+id/Visit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:onClick="VisitWebPage"
android:text="Visit" />
首先,在此示例中,您肯定不需要 RadioButtons 上的侦听器。
获得选中按钮的最佳选项来自PortalSelect.getCheckedRadioButtonId().
您将获得选中的 RadioButton 的 ID。
别忘了检查id是否存在;默认情况下,RadioGroup 初始化为选中 none 个 RadioButtons;
参考Google RadioGroup网站: http://developer.android.com/reference/android/widget/RadioGroup.html
在你的VisitWebPage
中先添加PortalSelect.getCheckedRadioButtonId()
赞
public void VisitWebPage(View v){
if(v.getId()==R.id.Visit){
int selectedId = PortalSelect.getCheckedRadioButtonId();
if(selectedId == MicrolabB.getId())
Log.d("RB Selected","1");
if(selectedId ==ISAB.getId())
Log.d("RB Selected","2");
if(selectedId ==MoodleB.getId())
Log.d("RB Selected","3");
//Intent intent = new Intent(Start.this,ApduNFC.class);
//startActivity(intent);
}
}
解决了问题。
public void VisitWebPage(View v){
final RadioGroup PortalSelect = (RadioGroup) findViewById(R.id.PortalSelect);
int ID = PortalSelect.getCheckedRadioButtonId();
if(v.getId()==R.id.Visit){
int rb1Id = MicrolabB.getId();
if(ID == MicrolabB.getId())
Log.d("RB Selected","1");
if(ID == ISAB.getId())
Log.d("RB Selected","2");
if(ID == MoodleB.getId())
Log.d("RB Selected","3");
Intent intent = new Intent(Start.this,ApduNFC.class);
startActivity(intent);
}
}
我输入行后
final RadioGroup PortalSelect = (RadioGroup) findViewById(R.id.PortalSelect);
在 VisitWebPage 函数中似乎一切正常。我不知道为什么,因为我之前在这个 class 中定义了 Radio Group。但我很高兴它现在可以正常工作了。