Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 两个放射组,每个放射组中只有一个选定的单选按钮_Java_Android_Radio Button_Radio Group - Fatal编程技术网

Java 两个放射组,每个放射组中只有一个选定的单选按钮

Java 两个放射组,每个放射组中只有一个选定的单选按钮,java,android,radio-button,radio-group,Java,Android,Radio Button,Radio Group,我的布局中有两个放射组。我希望在RadioGroup中一次只检查一个单选按钮。我试过很多方法,但没有一种能正常工作。现在我可以检查我组中的每个单选按钮 public class MainActivity extends ActionBarActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener { String x; char y; int c=1; String num,n,type; int

我的布局中有两个放射组。我希望在RadioGroup中一次只检查一个单选按钮。我试过很多方法,但没有一种能正常工作。现在我可以检查我组中的每个单选按钮

public class MainActivity extends ActionBarActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {

String x;
char y;

int c=1;

String num,n,type;
int d,b,o,h;
String dec,bin,hex,oct;

EditText number;
EditText result;

Button convert, clear;
RadioGroup rgFrom, rgTO;
int convertedResult;
int from=R.id.r10From;
int to=R.id.r2TO;

String stNumber; //Liczba przed przeliczeniem - string
int numbResult;  //Już int

String iterResult; //Liczba po przeliczeniu - string
int convResult;   //Już int

//int id = ((RadioGroup).findViewById( R.id.rgFrom )).getCheckedRadioButtonId();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initialize();


}

 private void initialize() {
   number = (EditText) findViewById(R.id.etNumber);
   result = (EditText) findViewById(R.id.etResult);

    convert = (Button) findViewById(R.id.bCalculate);
    clear = (Button) findViewById(R.id.bReset);

    convert.setOnClickListener(this);
    clear.setOnClickListener(this);

    rgFrom = (RadioGroup) findViewById(R.id.rgFrom);
    rgTO = (RadioGroup) findViewById(R.id.rgTO);


    rgFrom.setOnCheckedChangeListener(this);
    rgTO.setOnCheckedChangeListener(this);
}


@Override
public void onClick(View view) {
   // from = (String)findViewById(R.id.rgFrom).getSelectedItem();
   // int id = ((RadioGroup)findViewById( R.id.rgFrom )).getCheckedRadioButtonId();
    //from = getFrom(id);

    switch (view.getId()){
        case R.id.r2From:
            from = rgFrom.getCheckedRadioButtonId();
            break;
        case  R.id.r8From:
            from = rgFrom.getCheckedRadioButtonId();
            break;
        case  R.id.r10From:
            from = rgFrom.getCheckedRadioButtonId();
            break;
        case  R.id.r16From:
            from = rgFrom.getCheckedRadioButtonId();
            break;
        case R.id.r2TO:
            to = rgTO.getCheckedRadioButtonId();
            break;
        case  R.id.r8TO:
            to = rgTO.getCheckedRadioButtonId();
            break;
        case  R.id.r10TO:
            to = rgTO.getCheckedRadioButtonId();
            break;
        case  R.id.r16TO:
            to = rgTO.getCheckedRadioButtonId();
            break;
        case  R.id.bCalculate:
            stNumber = number.getText().toString();
            //iterResult = result.getText().toString();
            //calculate();
            break;
        case  R.id.bReset:
            //reset();
            break;
    }
}
@Override
public void onCheckedChanged(RadioGroup rgFrom, int i) {
    switch (view.getId()){
        case R.id.r2From:
            from = rgFrom.getCheckedRadioButtonId();
            break;
        case  R.id.r8From:
            from = rgFrom.getCheckedRadioButtonId();
            break;
        case  R.id.r10From:
            from = rgFrom.getCheckedRadioButtonId();
            break;
        case  R.id.r16From:
            from = rgFrom.getCheckedRadioButtonId();
            break;
        case R.id.r2TO:
            to = rgTO.getCheckedRadioButtonId();
            break;
        case  R.id.r8TO:
            to = rgTO.getCheckedRadioButtonId();
            break;
        case  R.id.r10TO:
            to = rgTO.getCheckedRadioButtonId();
            break;
        case  R.id.r16TO:
            to = rgTO.getCheckedRadioButtonId();
            break;
    }
   }
}
我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.converter_numeralsystem.app.MainActivity">

<TextView
    android:layout_marginTop="20dp"
    android:text="@string/number"
    android:textSize="20sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tvNumber" />

<TextView
    android:layout_marginTop="20dp"
    android:layout_below="@id/tvNumber"
    android:text="@string/result"
    android:textSize="20sp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/tvResult" />

<EditText
    android:id="@+id/etNumber"
    android:layout_marginLeft="5dp"
    android:hint="@string/enter_numb"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@id/tvNumber"
    android:layout_alignBottom="@id/tvNumber"/>
<EditText
    android:id="@+id/etResult"
    android:layout_marginLeft="5dp"
    android:hint="@string/et_result"
    android:singleLine="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@id/tvNumber"
    android:layout_alignBottom="@id/tvResult"/>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:text="@string/from"
    android:id="@+id/tvFrom"
    android:layout_below="@id/tvResult"
    android:layout_marginTop="20dp" />

<RadioGroup
    android:weightSum="100"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/tvFrom"
    android:id="@+id/rgFrom"
    android:layout_marginTop="10dp">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
    <RadioButton
        android:layout_weight="50"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/r2From"
        android:text="@string/dwa"/>


        <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/r8From"
        android:text="@string/osiem"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
         android:layout_marginRight="75dp" />

        <RadioButton
            android:checked="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/r10From"
            android:text="@string/dziesiec"
            android:layout_below="@id/r2From"
            android:layout_alignStart="@id/r2From" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/r16From"
            android:text="@string/szesnascie"
            android:layout_alignParentRight="true"
            android:layout_below="@id/r8From"
            android:layout_alignStart="@id/r8From"/>

    </RelativeLayout>
    </RadioGroup>

  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:text="@string/to"
    android:id="@+id/tvTo"
    android:layout_below="@id/rgFrom"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="20dp" />

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvTo"
    android:layout_marginTop="10dp"
    android:id="@+id/rgTO">

    <RelativeLayout
        android:id="@+id/rel2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:checked="true"
            android:layout_weight="50"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/r2TO"
            android:text="@string/dwa"/>

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/r8TO"
            android:text="@string/osiem"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_marginRight="75dp" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/r10TO"
            android:text="@string/dziesiec"
            android:layout_below="@id/r2TO"
            android:layout_alignStart="@id/r2TO" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/r16TO"
            android:text="@string/szesnascie"
            android:layout_alignParentRight="true"
            android:layout_below="@id/r8TO"
            android:layout_alignStart="@id/r8TO"/>

    </RelativeLayout>
    </RadioGroup>

<LinearLayout
android:layout_marginTop="10dp"
android:weightSum="100"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/rgTO">

<Button
    android:layout_weight="30"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/calculate"
    android:id="@+id/bCalculate"
    android:layout_toLeftOf="@+id/bReset" />

<Button
    android:layout_weight="70"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/reset"
    android:id="@+id/bReset"
    android:layout_alignBottom="@+id/bCalculate" />

</LinearLayout>


</RelativeLayout>

半径组
中删除
相对值。所以代码看起来像:

<RadioGroup
    android:id="@+id/rgFrom"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:weightSum="100" >

    <RadioButton
        android:id="@+id/r2From"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="50"
        android:text="dwa" />

    <RadioButton
        android:id="@+id/r8From"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="75dp"
        android:text="osiem" />

    <RadioButton
        android:id="@+id/r10From"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="dziesiec" />

    <RadioButton
        android:id="@+id/r16From"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="szesnascie" />
</RadioGroup>


这将解决在同一组中选中多个按钮所面临的问题。然而,这将在UI中产生一个小问题。我想你希望一行有两个按钮,另外两个按钮在不同的行。据我所知,您必须为此实现自己的自定义布局。查看详细信息。检查一下。希望能有点帮助。

也许这会有帮助?两个放射组都包含一个RelativeLayout(相同错误的两倍)。。。为什么?请尽快移除它们。谢谢,它很有效。但是我怎么才能创造出和我的亲戚一样的东西呢?