【发布时间】:2014-11-02 19:41:53
【问题描述】:
我正在尝试在 android studio/java 中创建一个复选框,如果选中该复选框,将导致其他复选框的子集可以选中。
开发者网站说: 抽象无效 setChecked(布尔检查) 更改视图的选中状态
但这对我不起作用。
当前代码:
Java:
package crc.cybereye;
public class CreateNew extends Activity {
LinearLayout background;
Button btnBack;
CheckBox checkbox_structural_info;
CheckBox checkbox_years_of;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
background = (LinearLayout) findViewById(R.id.background);
setContentView(R.layout.activity_create_new);
btnBack = (Button) findViewById(R.id.btnBack);
checkbox_years_of = (CheckBox) findViewById(R.id.checkbox_years_of);
checkbox_floors_above = (CheckBox) findViewById(R.id.checkbox_floors_above);
checkbox_structural_info = (CheckBox) findViewById(R.id.checkbox_structural_info);
// setUpIntroCheckBoxes(); // Add change listeners to check boxes
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), NewFormActivity.class);
startActivityForResult(intent, 0);
}
});
}
更多Java:
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkbox_structural_info:
if (checked){
checkbox_years_of checked.setChecked(true) //ERROR HERE
}
break;
case R.id.checkbox_years_of:
if (checked){
}
break;
XML:
<TableRow android:layout_marginTop="10dp">
<TextView
android:text="@string/structural_info"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
/>
<CheckBox android:id="@+id/checkbox_structural_info"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"/>
</TableRow>
<TableRow>
<TextView
android:text="@string/years_of"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
/>
<CheckBox android:id="@+id/checkbox_years_of"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"/>
<TableRow>
有一个错误说它无法解析方法 setChecked(boolean)。
我的代码目前只是试图让结构信息复选框检查years_of复选框是否被选中,但我也想让years_或仅在结构信息被选中时才可检查,我还没有到达那里,因为我仍然被卡住了在这个问题上。 非常感谢您提前提供的任何帮助。
【问题讨论】:
-
checkbox_years_of checked.setChecked(true) 不应该是 checkbox_years_of.setChecked(true) 吗? (你调用 setChecked 来检查它是一个布尔变量)
-
是的。谢谢(仍然掌握 Java 的窍门)
-
您可以发表您的评论作为答案,以便我可以检查它作为答案吗?或者有什么方法可以解决这个问题?