【发布时间】:2019-10-14 00:49:09
【问题描述】:
在我的应用程序中,有 2 个复选框来获取用户的类型(例如:学生或教师)。我知道我应该使用单选按钮而不是复选框,但我在教程中的问题要求使用复选框(所以请暂时忽略单选按钮)。
即使我选中了一个复选框,isChecked() 方法也会返回 false。
这里是xml
android:id="@+id/chkTeacher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="157dp"
android:layout_marginLeft="157dp"
android:layout_marginTop="56dp"
android:text="Teacher"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/regPassword" />
<CheckBox
android:id="@+id/chkStudent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="157dp"
android:layout_marginLeft="157dp"
android:layout_marginTop="20dp"
android:text="Student"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chkTeacher" />
这里是编码
EditText username, password;
CheckBox teacher, student;
DBHelper dbHelper = new DBHelper(this);
String type;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
username = findViewById(R.id.regUserName);
password = findViewById(R.id.regPassword);
teacher = (CheckBox) findViewById(R.id.chkTeacher);
student = (CheckBox) findViewById(R.id.chkStudent);
if (teacher.isChecked() == true){
type = "teacher";
}
else{
type = "student";
}
}
我该如何解决这个问题?感谢您宝贵的时间!
【问题讨论】:
-
but my question in tutorial asked to use a checkbox是的。但只有一个。不是两个。再试一次。重来。只有教师复选框可以。一切从阅读开始。