📜  如何使CardView在Android中可检查?

📅  最后修改于: 2021-05-13 17:50:34             🧑  作者: Mango

在Android中,我们可以使CardView可检查,这确实是一个有用的功能。如果我们希望用户选择一些项目并希望显示用户选择的项目,那么这对我们来说是最重要的功能。下面给出了一个示例GIF,以使我们对本文中要做的事情有一个了解。请注意,我们将使用Kotlin语言实施此项目。

使CardView在Android示例GIF中可检查

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Kotlin作为编程语言。

步骤2:使用Build.Gradle(应用程序级别)文件

导航到Gradle脚本> build.gradle(Module:app)并将以下依赖项添加到“依赖项”部分。

在plugins {}标签内添加kotlin android扩展。

步骤3:使用activity_main.xml文件

导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。

XML



  
    
    
  
        
  
        
  
            
            
  
                
  
                    
  
                    
  
                
  
            
  
            
            
  
                
  
                    
  
                    
                
  
            
              
        
          
        
        
  
            
  
                
  
                
  
            
        
  
        


Kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_main.*
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // implementing the long click listener 
        // for when pressed on card view
        cardTajMahal.setOnLongClickListener {
            // if the card view is checked,make 
            // it unchecked and vice-versa
            cardTajMahal.isChecked = !cardTajMahal.isChecked
            true
        }
  
        // implementing the long click listener 
        // for when pressed on card view
        cardStatueOfUnity.setOnLongClickListener {
            cardStatueOfUnity.isChecked = !cardStatueOfUnity.isChecked
            true
        }
  
        // implementing the long click listener
        // for when pressed on card view
        cardLotusTemple.setOnLongClickListener {
            cardLotusTemple.isChecked = !cardLotusTemple.isChecked
            true
        }
  
        btnWhatsSelected.setOnClickListener {
            // getting the info about which 
            // card view has been selected
            var msg = ""
            if (cardTajMahal.isChecked) {
                msg += "'Taj Mahal' "
            }
            if (cardStatueOfUnity.isChecked) {
                msg += "'Statue Of Unity' "
            }
            if (cardTajMahal.isChecked) {
                msg += "'Lotus Temple' "
            }
            // snack bar is just like a toast msg
            Snackbar.make(it, " $msg Selected", Snackbar.LENGTH_LONG).show()
        }
    }
}


步骤4:使用MainActivity.kt文件

转到MainActivity.kt文件,并参考以下代码。下面是MainActivity.kt文件的代码。在代码内部添加了注释,以更详细地了解代码。

科特林

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.google.android.material.snackbar.Snackbar
import kotlinx.android.synthetic.main.activity_main.*
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // implementing the long click listener 
        // for when pressed on card view
        cardTajMahal.setOnLongClickListener {
            // if the card view is checked,make 
            // it unchecked and vice-versa
            cardTajMahal.isChecked = !cardTajMahal.isChecked
            true
        }
  
        // implementing the long click listener 
        // for when pressed on card view
        cardStatueOfUnity.setOnLongClickListener {
            cardStatueOfUnity.isChecked = !cardStatueOfUnity.isChecked
            true
        }
  
        // implementing the long click listener
        // for when pressed on card view
        cardLotusTemple.setOnLongClickListener {
            cardLotusTemple.isChecked = !cardLotusTemple.isChecked
            true
        }
  
        btnWhatsSelected.setOnClickListener {
            // getting the info about which 
            // card view has been selected
            var msg = ""
            if (cardTajMahal.isChecked) {
                msg += "'Taj Mahal' "
            }
            if (cardStatueOfUnity.isChecked) {
                msg += "'Statue Of Unity' "
            }
            if (cardTajMahal.isChecked) {
                msg += "'Lotus Temple' "
            }
            // snack bar is just like a toast msg
            Snackbar.make(it, " $msg Selected", Snackbar.LENGTH_LONG).show()
        }
    }
}

输出:

想要一个节奏更快,更具竞争性的环境来学习Android的基础知识吗?
单击此处前往由我们的专家精心策划的指南,以使您立即做好行业准备!