我下面有简单的代码,我不明白为什么它对我不起作用.
我只想做的就是根据我单击的单选按钮来显示(吐司)“男”或“女”.
这是主要活动的代码:
private static RadioGroup radio_g;private static Radiobutton radio_b;private static button button_sbm;public voID onClickListenerbutton() { radio_g = (RadioGroup)findVIEwByID(R.ID.genderbutton); button_sbm = (button)findVIEwByID(R.ID.getBMI); button_sbm.setonClickListener( new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { int selected_ID = radio_g.getCheckedRadiobuttonID(); radio_b = (Radiobutton)findVIEwByID(selected_ID); Toast.makeText(MainActivity.this, radio_b.getText().toString(),Toast.LENGTH_SHORT ).show(); } } );}
和广播组:
<RadioGroup androID:ID="@+ID/genderbutton" androID:layout_wIDth="124dp" androID:layout_height="67dp" androID:layout_marginleft="89dp" androID:layout_marginStart="88dp" androID:layout_margintop="63dp" androID:visibility="visible" app:layout_constraintleft_toleftOf="parent" app:layout_constrainttop_toBottomOf="@+ID/ageFIEld" tools:layout_constraintleft_creator="1" tools:layout_constrainttop_creator="1"> <Radiobutton androID:ID="@+ID/malebutton" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_weight="1" androID:checked="false" androID:text="Male" tools:text="male" /> <Radiobutton androID:ID="@+ID/femalebutton" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_weight="1" androID:text="Female" tools:text="female" /></RadioGroup>
有人可以指出我所缺少的吗?这真令人沮丧.
解决方法:
您的代码必须如下所示:
public class FormActivity extends Activity implements VIEw.OnClickListener {private static RadioGroup radio_g;private static Radiobutton radio_b;private static button button_sbm; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_form); // initialize component here like this radio_g = (RadioGroup)findVIEwByID(R.ID.genderbutton); button_sbm = (button)findVIEwByID(R.ID.getBMI); // Then call Listener on button click like this button_sbm .setonClickListener(this); } @OverrIDe public voID onClick(VIEw v) { int selected_ID = radio_g.getCheckedRadiobuttonID(); radio_b = (Radiobutton)findVIEwByID(selected_ID); Toast.makeText(MainActivity.this, radio_b.getText().toString(),Toast.LENGTH_SHORT ).show(); }
试试这个,我尝试一下很好.
总结以上是内存溢出为你收集整理的android-无法在单选按钮组中显示按钮的值全部内容,希望文章能够帮你解决android-无法在单选按钮组中显示按钮的值所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)