Android 收音机盒不工作,并且给出了错误的值
我有一个android项目,我有一个活动,它在同一个广播组中包含两个文本视图和两个广播框,我试图做的是当我按下第一个广播框时,第二个文本视图不可见,当另一个广播框被选中时,第一个文本视图不可见 这是我的密码 这是我的布局 但是我有两个问题,第一个是:两个收音机盒子可以同时检查!!!这是错误的,另一个是当idx值被打印出来时,它给了我-1,这很奇怪。。有人能帮我吗?试试这个代码Android 收音机盒不工作,并且给出了错误的值,android,radio-button,Android,Radio Button,我有一个android项目,我有一个活动,它在同一个广播组中包含两个文本视图和两个广播框,我试图做的是当我按下第一个广播框时,第二个文本视图不可见,当另一个广播框被选中时,第一个文本视图不可见 这是我的密码 这是我的布局 但是我有两个问题,第一个是:两个收音机盒子可以同时检查!!!这是错误的,另一个是当idx值被打印出来时,它给了我-1,这很奇怪。。有人能帮我吗?试试这个代码 RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup);
RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.radio0:
textView2.setVisibility(View.INVISIBLE);
active=1;
break;
case R.id.radio1:
textView.setVisibility(View.INVISIBLE);
active=2;
break;
}
}
});
您的问题是,单选按钮不是RadioGroup的直接子项,而是TableRow的子项,因此这不会将您的选择限制为1个单选按钮。更改布局,使它们都是RadioGroup下的直接子项。如果您希望它们位于彼此之下,则可以使用RelativeLayout获得相同的结果。请添加布局xml
<RelativeLayout
android:id="@+id/relative2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/relative1"
android:background="@drawable/down"
android:focusableInTouchMode="true"
android:focusable="true" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5sp"
android:paddingRight="7dp"
android:text="الرجاء اختيار اسم العميل "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="30sp" />
</TableRow>
<RadioGroup
android:id="@+id/radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_centerHorizontal="true"
>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_marginRight="10sp" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:text="الصيدليات"
android:textSize="28sp"/>
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginLeft="30sp"
android:layout_marginRight="30sp">
<AutoCompleteTextView android:id="@+id/txtsearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:imeOptions="actionDone"
android:layout_weight="1"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_marginRight="10sp" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:text="العملاء"
android:textSize="28sp" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</TableRow>
</RadioGroup>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginLeft="30sp"
android:layout_marginRight="30sp">
<AutoCompleteTextView android:id="@+id/txtsearch2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"
android:imeOptions="actionDone"
android:layout_weight="1"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/button1"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_marginBottom="20sp"
android:layout_marginTop="15sp"
android:background="#ff9900"
android:text="متابعة"
android:layout_marginLeft="70dp"
android:layout_marginRight="70dp"
android:textSize="28sp" />
</TableRow>
</TableLayout>
</RelativeLayout>
RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup);
rg.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId){
case R.id.radio0:
textView2.setVisibility(View.INVISIBLE);
active=1;
break;
case R.id.radio1:
textView.setVisibility(View.INVISIBLE);
active=2;
break;
}
}
});