2016-12-09 2 views
0

グリッド内の4つのラジオボタンで1つの質問と4つの回答を持つ簡単なアプリのレイアウトを作成しました。私は2つのラジオグループを使ってレイアウトを設計しました。 1つのラジオグループを使って同じデザインを手伝ってください。事前に感謝しますThis is what my layout looks like, but I have accomplished this using two radio groups one after another using two radio buttons in eachグリッドのラジオボタンのレイアウト

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 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:orientation="vertical" 
tools:context="com.example.android.educationalapp.MainActivity"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/Q1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     android:layout_marginTop="8dp" 
     android:paddingLeft="8dp" 
     android:paddingRight="8dp" 
     android:text="@string/Q1" /> 

    <RadioGroup 
     android:id="@+id/R1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/Q1" 
     android:layout_marginTop="8dp" 
     android:orientation="horizontal"> 


     <RadioButton 
      android:id="@+id/A1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:paddingLeft="4dp" 
      android:paddingRight="4dp" 
      android:text="@string/A1" /> 

     <RadioButton 
      android:id="@+id/B1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:paddingLeft="4dp" 
      android:paddingRight="4dp" 
      android:text="@string/B1" /> 
    </RadioGroup> 

    <RadioGroup 
     android:id="@+id/R2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/R1" 
     android:layout_marginTop="8dp" 
     android:orientation="horizontal"> 


     <RadioButton 
      android:id="@+id/C1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:paddingLeft="4dp" 
      android:paddingRight="4dp" 
      android:text="@string/C1" /> 

     <RadioButton 
      android:id="@+id/D1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:paddingLeft="4dp" 
      android:paddingRight="4dp" 
      android:text="@string/D1" /> 
    </RadioGroup> 

    <Button 
     android:id="@+id/S1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@id/R2" 
     android:layout_marginTop="4dp" 
     android:layout_marginRight="4dp" 
     android:text="Check" /> 

    <View 
     android:id="@+id/V1" 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:layout_below="@id/S1" 
     android:layout_marginTop="8dp" 
     android:background="@color/colorPrimaryDark"> 

    </View> 


    </RelativeLayout> 

    <RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <TextView 
     android:id="@+id/Q2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="8dp" 
     android:layout_marginTop="8dp" 
     android:paddingLeft="8dp" 
     android:paddingRight="8dp" 
     android:text="@string/Q2" /> 

    <RadioGroup 
     android:id="@+id/R3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/Q2" 
     android:layout_marginTop="8dp" 
     android:orientation="horizontal"> 


     <RadioButton 
      android:id="@+id/A2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:paddingLeft="4dp" 
      android:paddingRight="4dp" 
      android:text="@string/A2" /> 

     <RadioButton 
      android:id="@+id/B2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:paddingLeft="4dp" 
      android:paddingRight="4dp" 
      android:text="@string/B2" /> 
    </RadioGroup> 

    <RadioGroup 
     android:id="@+id/R4" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/R3" 
     android:layout_marginTop="8dp" 
     android:orientation="horizontal"> 


     <RadioButton 
      android:id="@+id/C2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:paddingLeft="4dp" 
      android:paddingRight="4dp" 
      android:text="@string/C2" /> 

     <RadioButton 
      android:id="@+id/D2" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:paddingLeft="4dp" 
      android:paddingRight="4dp" 
      android:text="@string/D2" /> 
    </RadioGroup> 

    <Button 
     android:id="@+id/S2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_below="@id/R4" 
     android:layout_marginTop="4dp" 
     android:text="Check" 
     android:layout_marginRight="4dp" 
     android:onClick="onCheckButtonClicked"/> 

    <View 
     android:id="@+id/V2" 
     android:layout_width="match_parent" 
     android:layout_height="1dp" 
     android:layout_below="@id/S2" 
     android:layout_marginTop="8dp" 
     android:background="@color/colorPrimaryDark"> 

    </View> 


</RelativeLayout> 


</LinearLayout> 

これは私のXMLコードのようです。私が望むのは、質問ごとに一度にラジオボタンを1つだけ選択しなければならないということです。私は1つのラジオ・グループを使ってコードを知っていますが、2つのラジオ・グループではわかりません。ご意見をお聞かせください。

+0

どのように掲示について君はrコード?それは簡単に答える。 – Blackbam

+0

@Blackbam XMLコードを共有しました。あなたが私を助けることができれば感謝します。 –

+0

助けが必要な人は誰でも助けてください。 –

答えて

-1

次のアプローチは私には役に立ちます。これは、Android開発者のためのUdacity MOOCマテリアルデザインに基づいています。これには、9つのラジオボタンのグループが2列に配置されたImmersiveImagesというデモアプリケーションが含まれています。以下のコードは、グリッド1行x 2列を生成するこのアプリの簡略版です。より大きなグリッドに簡単に拡張できます。

activity_main.xml

<FrameLayout 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" 
      tools:context=".MainActivity"> 

     <GridLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_alignParentStart="true" 
      android:layout_marginTop="16dp" 
      android:paddingRight="16dp"> 

     <RadioButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="CENTER" 
      android:id="@+id/centerBtn" 
      android:layout_column="0" 
      android:layout_row="0" /> 

     <RadioButton 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="FIT_END" 
      android:id="@+id/fitEndBtn" 
      android:layout_row="0" 
      android:layout_column="2" /> 

     </GridLayout> 

</FrameLayout> 

参考

public class MainActivity extends AppCompatActivity implements View.OnClickListener 
{ 

    RadioButton centerBtn; 
    RadioButton fitEndBtn; 

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

    centerBtn = (RadioButton) findViewById(R.id.centerBtn); 
    fitEndBtn = (RadioButton) findViewById(R.id.fitEndBtn); 

    centerBtn.setOnClickListener(this); 
    fitEndBtn.setOnClickListener(this); 

    centerBtn.setChecked(true); 

    } 

    /** 
    * Obtain reference to selected button 
    * @param view The most recently clicked radio button. 
    * @return The clicked radio button if different to parameter view. 
    */ 
    public RadioButton getSelectedRadio(View view) { 
    RadioButton[] btns = {centerBtn, fitEndBtn}; 
    for (RadioButton radioButton : btns) { 
     if (radioButton.isChecked() && radioButton != view) { 
     return radioButton; 
     } 
    } 
    return null; 
    } 

    @Override 
    public void onClick(View view) { 

    // Determine if a radio button clicked and implement handler 
    RadioButton checkedRadio = view instanceof RadioButton ? getSelectedRadio(view) : null; 
    if (checkedRadio != null) { 
     // Untick all buttons except currently selected. 
     if (checkedRadio != view) { 
     checkedRadio.setChecked(false); 
     } 
     String radioId = ((RadioButton) view).getText().toString(); 
     switch (radioId) { 
     case "CENTER": 
      Toast.makeText(this, "Button CENTER selected", Toast.LENGTH_SHORT).show(); 
      break; 
     case "FIT_END": 
      Toast.makeText(this, "Button FIT_END selected", Toast.LENGTH_SHORT).show(); 
      break; 
     } 
    } 
    else { 
     // Other view handlers 
    } 
    } 
} 

MainActivity.java、アプリはここからダウンロードして入手可能です:
https://github.com/usplitu/android_grid_radio_buttons

関連する問題