📱 Checkbox en Android Studio [21]
En este tutorial vamos a aprender a usar los Checkbox en Android Studio haciendo un simple ejercicio con estos pasos:
- Insertamos un TextView y cuatro Checkbox.
- En cada Checkbox colocaremos un lenguaje de programación.
- Al seleccionar en algun Checkbox agregaremos ese lenguaje de programación a nuestro TextView.
- Al deseleccionar en algun Checkbox quitaremos ese lenguaje de programación a nuestro TextView.
package com.programadornovato.miprimerproyecto
import android.content.DialogInterface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AlertDialog
import java.math.BigDecimal
import kotlin.math.PI
import kotlin.math.round
class MainActivity : AppCompatActivity() {
private var chKotlin:CheckBox?=null
private var chJava:CheckBox?=null
private var chC:CheckBox?=null
private var chPHP:CheckBox?=null
private var tvSeleccion:TextView?=null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
chKotlin=findViewById(R.id.chKotlin)
chJava=findViewById(R.id.chJava)
chC=findViewById(R.id.chC)
chPHP=findViewById(R.id.chPHP)
tvSeleccion=findViewById(R.id.tvSeleccion)
}
fun accion(view: View){
var seleccion=""
if(chKotlin?.isChecked==true){
seleccion=seleccion+"Usted selecciono ${chKotlin?.text}\n"
}
if(chJava?.isChecked==true){
seleccion=seleccion+"Usted selecciono ${chJava?.text}\n"
}
if(chC?.isChecked==true){
seleccion=seleccion+"Usted selecciono ${chC?.text}\n"
}
if(chPHP?.isChecked==true){
seleccion=seleccion+"Usted selecciono ${chPHP?.text}\n"
}
if(seleccion.isEmpty()==true){
tvSeleccion?.text="Humano seleccionar un lenguaje!!!!"
}else{
tvSeleccion?.text=seleccion
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:text="Humano selecciona un lenguaje de programacion"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvSeleccion"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Humano seleccionar un lenguaje!!!!"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chPHP" />
<CheckBox
android:id="@+id/chKotlin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="24dp"
android:onClick="accion"
android:text="Kotlin"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<CheckBox
android:id="@+id/chJava"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="8dp"
android:onClick="accion"
android:text="Java"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chKotlin" />
<CheckBox
android:id="@+id/chC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="8dp"
android:onClick="accion"
android:text="C++"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chJava" />
<CheckBox
android:id="@+id/chPHP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="8dp"
android:onClick="accion"
android:text="PHP"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/chC" />
</androidx.constraintlayout.widget.ConstraintLayout>
🎦 Lista de reproducción de este curso: https://www.youtube.com/watch?v=3ZUlE_tvfaM&list=PLCTD_CpMeEKRL2-HaZThebGcyyR3_RshO&ab_channel=programadornovato
🎦 Curso de Android Studio: https://www.youtube.com/watch?v=AHyTqIB_8Rs&list=PLCTD_CpMeEKTT9pb9J-89j1SPtqHzU7sF&ab_channel=programadornovato
🎦 [Curso] Java Netbeans Completo☕: https://www.youtube.com/playlist?list=PLCTD_CpMeEKTT-qEHGqZH3fkBgXH4GOTF
🎦 [Curso] Kotlin de 0 a 100: https://www.youtube.com/playlist?list=PLCTD_CpMeEKSjzbsW_zmVNz23GyOVsdbS
🎦 [Curso] Java Netbeans Completo☕: https://www.youtube.com/playlist?list=PLCTD_CpMeEKTT-qEHGqZH3fkBgXH4GOTF
🔗 Facebook: https://facebook.com/ProgramadorNovatoOficial
🔗 Twitter: https://twitter.com/programadornova
🔗 Linkedin: https://www.linkedin.com/in/programadornovato/
🔗 Instagram: https://www.instagram.com/programadornovato/
🔗 Pagina oficial: https://www.programadornovato.com
🔗 Gracias por apoyar este canal: https://www.patreon.com/programadornovato
Anterior tutorial Siguiente tutorial