[英]isChecked is not working on checkbox Programmatically on Kotlin
我正在尝试使用 ischeked 函数填充复选框,但看起来无论我做什么都不起作用,我尝试过使用 .clearFocus()、toggle()、jumpDrawablesToCurrentState() 但任何这些方法都有效,我试过在创建复选框后填充它,稍后当我比较来自 firebase 的结果但它在任何情况下都不起作用时,这是我的完整代码,希望您能帮助我。
private var mNameField: EditText? = null
private var mPhoneField: EditText? = null
private var mBack: Button? = null
private var mConfirm: Button? = null
private var mProfileImage: ImageView? = null
private var mAuth: FirebaseAuth? = null
private var mUserDatabase: DatabaseReference? = null
private var userId: String? = null
private var name: String? = null
private var biografia: String? = null
private var profileImageUrl: String? = null
private var userSex: String? = null
private var resultUri: Uri? = null
private var mRadioGroupBuscar: RadioGroup? = null
private var valormRadioGroupBuscar: RadioGroup? = null
private lateinit var radioButtonBuscaSexo: RadioButton
private var PC: CheckBox?=null
private var Playstation: CheckBox?=null
private var Xbox : CheckBox?=null
private var Nintendo : CheckBox?=null
private var Movil: CheckBox?=null
private var aString: String?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_settings)
mNameField = findViewById<View>(R.id.name) as EditText
mPhoneField = findViewById<View>(R.id.phone) as EditText
mProfileImage = findViewById<View>(R.id.profileImage) as ImageView
mBack = findViewById<View>(R.id.back) as Button
mConfirm = findViewById<View>(R.id.confirm) as Button
mAuth = FirebaseAuth.getInstance()
userId = mAuth?.currentUser!!.uid
mUserDatabase = FirebaseDatabase.getInstance().reference.child("Users").child(userId!!)
mRadioGroupBuscar = findViewById<View>(R.id.radioGroupBusca) as RadioGroup
val selectIdBuscaSexo = mRadioGroupBuscar!!.checkedRadioButtonId
//val radioButtonBuscaSexo = findViewById<View>(selectIdBuscaSexo) as RadioButton
PC = findViewById<View>(R.id.checkBoxPc) as CheckBox?
Xbox = findViewById<View>(R.id.checkBoxXbox) as CheckBox?
Nintendo = findViewById<View>(R.id.checkBoxNintendo) as CheckBox?
Movil = findViewById<View>(R.id.checkBoxMovil) as CheckBox?
Playstation = findViewById<View>(R.id.checkBoxPlaytation) as CheckBox?
//I HAVE TRIED TO CHECK MY CHECKBOX HERE
// Playstation?.jumpDrawablesToCurrentState()
// Playstation?.isChecked = true
Playstation!!.clearFocus()
Playstation!!.isChecked= true;
userInfo
mProfileImage!!.setOnClickListener {
val intent = Intent(Intent.ACTION_PICK)
intent.type = "image/*"
startActivityForResult(intent, 1)
}
mConfirm!!.setOnClickListener { saveUserInformation() }
mBack!!.setOnClickListener(View.OnClickListener {
finish()
return@OnClickListener
})
}
private val userInfo: Unit
private get() {
mUserDatabase!!.addListenerForSingleValueEvent(object : ValueEventListener {
override fun onDataChange(dataSnapshot: DataSnapshot) {
if (dataSnapshot.exists() && dataSnapshot.childrenCount > 0) {
val map = dataSnapshot.value as Map<String, Any?>?
if (map!!["Name"] != null) {
name = map["Name"].toString()
mNameField!!.setText(name)
}
if (map["biografia"] != null) {
biografia = map["biografia"].toString()
mPhoneField!!.setText(biografia)
}
if (map["sexo"] != null) {
userSex = map["sexo"].toString()
}
if (map["Playstation"].toString() != "false") {
var aString = map["Playstation"].toString()
//HERE I tried to Cheked
Playstation?.isChecked
Playstation?.isChecked = true
Playstation?.jumpDrawablesToCurrentState()
}
Glide.with(mProfileImage!!.context).clear(mProfileImage!!)
if (map["profileImageUrl"] != null) {
profileImageUrl = map["profileImageUrl"].toString()
when (profileImageUrl) {
"default" -> Glide.with(application).load(R.mipmap.ic_launcher).into(mProfileImage!!)
else -> Glide.with(application).load(profileImageUrl).into(mProfileImage!!)
}
}
}
}
override fun onCancelled(databaseError: DatabaseError) {}
})
}
private fun saveUserInformation() {
name = mNameField!!.text.toString()
biografia = mPhoneField!!.text.toString()
var buscasex = radioButtonBuscaSexo.text.toString()
val userInfo: java.util.HashMap<Any?, Any?> = java.util.HashMap<Any?, Any?>()
userInfo["Name"] = name
userInfo["biografia"] = biografia
// userInfo["buscarSexo"] = buscasex
// userInfo["PC"] = checkIfItsChecked(PC!!)
userInfo["Playstation"] = checkIfItsCheckedString(aString!!)
// userInfo["Xbox"] = checkIfItsChecked(Xbox!!)
// userInfo["Nintendo"] = checkIfItsChecked(Nintendo!!)
// userInfo["Movile"] = checkIfItsChecked(Movil!!)
mUserDatabase?.updateChildren(userInfo as Map<String, Any>)
val a = 1
if (resultUri != null) {
val filepath = FirebaseStorage.getInstance().reference.child("profileImages").child(userId!!)
var bitmap: Bitmap? = null
try {
bitmap = MediaStore.Images.Media.getBitmap(application.contentResolver, resultUri)
} catch (e: IOException) {
e.printStackTrace()
}
val baos = ByteArrayOutputStream()
bitmap!!.compress(Bitmap.CompressFormat.JPEG, 20, baos)
val data = baos.toByteArray()
val uploadTask = filepath.putBytes(data)
uploadTask.addOnFailureListener { finish() }
uploadTask.addOnSuccessListener(OnSuccessListener { taskSnapshot ->
val downloadUrl = taskSnapshot.storage.downloadUrl
while (!downloadUrl.isSuccessful);
val resultdowloadUrl: Uri? = downloadUrl.getResult()
val userInfo: MutableMap<String, Any?> = HashMap<String, Any?>()
Log.e("uri12",resultdowloadUrl.toString()+"This is uri of image download");
Toast.makeText(this, resultdowloadUrl.toString(), Toast.LENGTH_SHORT).show();
userInfo.set("profileImageUrl", resultdowloadUrl.toString())
mUserDatabase!!.updateChildren(userInfo as Map<String, Any>)
finish()
return@OnSuccessListener
})
} else {
finish()
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
val imageUri = data?.data
resultUri = imageUri
mProfileImage!!.setImageURI(resultUri)
}
}
private fun checkIfItsChecked(checkBox: CheckBox): Boolean{
return checkBox.isChecked
}
private fun checkIfItsCheckedString(string: String): Boolean{
return string == "true"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.