【发布时间】:2016-04-26 17:45:09
【问题描述】:
我对 Android 完全陌生
我尝试了任何我认为可行的方法,但每次都出错。这里有什么问题?
main_activity.xml
<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:padding="16dp"
tools:context=".Main"
android:orientation="vertical"
android:id="@+id/mainLayout">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton1" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton2" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton3" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton4" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton5" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton6" />
</LinearLayout>
</LinearLayout>
Main.java
public class Main extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
RadioButton rb1 = (RadioButton) findViewById(R.id.radioButton1);
RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2);
RadioButton rb3 = (RadioButton) findViewById(R.id.radioButton3);
RadioButton rb4 = (RadioButton) findViewById(R.id.radioButton4);
RadioButton rb5 = (RadioButton) findViewById(R.id.radioButton5);
RadioButton rb6 = (RadioButton) findViewById(R.id.radioButton6);
RadioGroup rg = new RadioGroup(this);
rg.addView(rb1);
rg.addView(rb2);
rg.addView(rb3);
rg.addView(rb4);
rg.addView(rb5);
rg.addView(rb6);
}
}
事实上,我正在尝试创建一个具有 3 列和 2 行的无线电组,但是当我这样做时,它不起作用:
main_activity.xml
<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:padding="16dp"
tools:context=".Main"
android:orientation="vertical"
android:id="@+id/mainLayout">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton1" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton2" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton3" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton4" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton5" />
<RadioButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="New RadioButton"
android:id="@+id/radioButton6" />
</LinearLayout>
</RadioGroup>
</LinearLayout>
我得到的最后一个解决方案是以编程方式在Main.java 中创建一个广播组,但它在rg.addView() 方法上出错
【问题讨论】:
-
RadioGroup rg = new RadioGroup(this);。不,您应该在 xml 文件中定义一个空的 RadioGroup。然后在运行时创建 RadioButtons 并将它们添加到 RadioGroup。
标签: java android radio-button radio-group