我为一个简单的应用程序创建了一个布局,它在网格中的4个单选按钮中有一个问题和4个答案。我使用2个Radio组设计了布局。请帮我设计一个无线电组。提前致谢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代码的样子。我想要的是每个问题一次只能选择一个单选按钮。我知道使用一个无线电组的代码,但对于两个无线电组我无法弄清楚。请指教。
答案 0 :(得分:-1)
以下方法适合我。它基于适用于Android开发者的Udacity MOOC Material Design,其中包含一个名为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>
<强> MainActivity.java 强>
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
}
}
}
供参考,该应用可从此处下载:
https://github.com/usplitu/android_grid_radio_buttons