如果在 Kotlin 中选中了复选框,则启用按钮?
Enable button if checkbox is selected in Kotlin?
我想在选中或选中复选框时启用按钮,在未选中或未选中时禁用按钮。
我使用 kotlin 启动了我的所有应用程序。
如果有一种方法可以在科特林中做类似的事情,请帮忙!
如果在 kotlin 中没有办法,请告诉我一种方法来完成我上面提到的想要的事情。
感谢谁在努力提供帮助。
see this picture of my mean
这是我的按钮和复选框代码以及:
<LinearLayout
android:id="@+id/llIAWith"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:orientation="horizontal"
android:padding="10dp">
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/i_agree_with"
android:textSize="15dp"
android:textColor="@color/Middle_Gray"
app:buttonTint="@color/Orange"
tools:ignore="TextContrastCheck" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/terms_of_services"
android:textSize="15dp"
android:textColor="@color/Orange"
tools:ignore="TextContrastCheck" />
</LinearLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnSUp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="30dp"
android:background="@drawable/custom_button"
android:text="@string/sign_up"
android:textSize="18dp"
android:textAllCaps="false"
android:textColor="@color/White"
tools:ignore="TextContrastCheck,TouchTargetSizeCheck" />
这是我的 kotlin 编解码器:
class LoginActivity : AppCompatActivity() {
private lateinit var binding: ActivityLoginBinding
private lateinit var auth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityLoginBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
initData()
}
private fun checkForInternet(context: Context): Boolean {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val network = connectivityManager.activeNetwork ?: return false
val activeNetwork = connectivityManager.getNetworkCapabilities(network) ?: return false
return when{
activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
activeNetwork.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) -> true
else -> false
}
}
private fun initData(){
auth = FirebaseAuth.getInstance()
clickListener()
clickListenerFP()
}
private fun clickListenerFP(){
binding.llFPassword.setOnClickListener{
startActivity(Intent(this,ForgotPasswordActivity::class.java))
finish()
}
}
private fun clickListener(){
binding.llDHAAccount.setOnClickListener{
startActivity(Intent(this,RegisterActivity::class.java))
finish()
}
binding.btnSIn.setOnClickListener{
getUserData()
}
}
private fun getUserData(){
val email = binding.etEmail.text.toString()
val password = binding.etPassword.text.toString()
if(email.isNotEmpty() && password.isNotEmpty()){
authUser(email, password)
}else{
Toast.makeText(this,resources.getString(R.string.all_inputs_are_required),Toast.LENGTH_SHORT).show()
}
}
private fun authUser(email: String, password: String){
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener{
checkResult(it.isSuccessful)
}
}
private fun checkResult(isSuccess: Boolean){
if (checkForInternet(this)) {
Toast.makeText(this,resources.getString(R.string.connected), Toast.LENGTH_SHORT).cancel()
} else {
Toast.makeText(this,resources.getString(R.string.no_network_connection), Toast.LENGTH_SHORT).show()
}
if(isSuccess){
startActivity(Intent(this,DashboardActivity::class.java))
finish()
}else{
Toast.makeText(this,resources.getString(R.string.authentication_failed),Toast.LENGTH_SHORT).show()
}
}
private var backPressedTime:Long = 0
private lateinit var backToast:Toast
override fun onBackPressed() {
backToast = Toast.makeText(this,resources.getString(R.string.press_back_again_to_leave_the_app), Toast.LENGTH_SHORT)
if (backPressedTime + 2000 > System.currentTimeMillis()) {
backToast.cancel()
super.onBackPressed()
return
} else {
backToast.show()
}
backPressedTime = System.currentTimeMillis()
}
这里是
binding.cb.setOnCheckedChangeListener { _, isChecked ->
binding.btnSUp.isEnabled = isChecked
}
我想在选中或选中复选框时启用按钮,在未选中或未选中时禁用按钮。 我使用 kotlin 启动了我的所有应用程序。 如果有一种方法可以在科特林中做类似的事情,请帮忙! 如果在 kotlin 中没有办法,请告诉我一种方法来完成我上面提到的想要的事情。 感谢谁在努力提供帮助。 see this picture of my mean
这是我的按钮和复选框代码以及:
<LinearLayout
android:id="@+id/llIAWith"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:orientation="horizontal"
android:padding="10dp">
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/i_agree_with"
android:textSize="15dp"
android:textColor="@color/Middle_Gray"
app:buttonTint="@color/Orange"
tools:ignore="TextContrastCheck" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/terms_of_services"
android:textSize="15dp"
android:textColor="@color/Orange"
tools:ignore="TextContrastCheck" />
</LinearLayout>
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/btnSUp"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="30dp"
android:background="@drawable/custom_button"
android:text="@string/sign_up"
android:textSize="18dp"
android:textAllCaps="false"
android:textColor="@color/White"
tools:ignore="TextContrastCheck,TouchTargetSizeCheck" />
这是我的 kotlin 编解码器:
class LoginActivity : AppCompatActivity() {
private lateinit var binding: ActivityLoginBinding
private lateinit var auth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityLoginBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
initData()
}
private fun checkForInternet(context: Context): Boolean {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val network = connectivityManager.activeNetwork ?: return false
val activeNetwork = connectivityManager.getNetworkCapabilities(network) ?: return false
return when{
activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
activeNetwork.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
activeNetwork.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) -> true
else -> false
}
}
private fun initData(){
auth = FirebaseAuth.getInstance()
clickListener()
clickListenerFP()
}
private fun clickListenerFP(){
binding.llFPassword.setOnClickListener{
startActivity(Intent(this,ForgotPasswordActivity::class.java))
finish()
}
}
private fun clickListener(){
binding.llDHAAccount.setOnClickListener{
startActivity(Intent(this,RegisterActivity::class.java))
finish()
}
binding.btnSIn.setOnClickListener{
getUserData()
}
}
private fun getUserData(){
val email = binding.etEmail.text.toString()
val password = binding.etPassword.text.toString()
if(email.isNotEmpty() && password.isNotEmpty()){
authUser(email, password)
}else{
Toast.makeText(this,resources.getString(R.string.all_inputs_are_required),Toast.LENGTH_SHORT).show()
}
}
private fun authUser(email: String, password: String){
auth.signInWithEmailAndPassword(email, password)
.addOnCompleteListener{
checkResult(it.isSuccessful)
}
}
private fun checkResult(isSuccess: Boolean){
if (checkForInternet(this)) {
Toast.makeText(this,resources.getString(R.string.connected), Toast.LENGTH_SHORT).cancel()
} else {
Toast.makeText(this,resources.getString(R.string.no_network_connection), Toast.LENGTH_SHORT).show()
}
if(isSuccess){
startActivity(Intent(this,DashboardActivity::class.java))
finish()
}else{
Toast.makeText(this,resources.getString(R.string.authentication_failed),Toast.LENGTH_SHORT).show()
}
}
private var backPressedTime:Long = 0
private lateinit var backToast:Toast
override fun onBackPressed() {
backToast = Toast.makeText(this,resources.getString(R.string.press_back_again_to_leave_the_app), Toast.LENGTH_SHORT)
if (backPressedTime + 2000 > System.currentTimeMillis()) {
backToast.cancel()
super.onBackPressed()
return
} else {
backToast.show()
}
backPressedTime = System.currentTimeMillis()
}
这里是
binding.cb.setOnCheckedChangeListener { _, isChecked ->
binding.btnSUp.isEnabled = isChecked
}