📜  如何在Android中创建解锁滑动条?

📅  最后修改于: 2021-05-09 18:00:36             🧑  作者: Mango

SeekBar是ProgressBar的扩展,它添加了可拖动的滑块。用户可以触摸拇指并向左或向右拖动以设置当前进度级别,或使用箭头键。 SeekBar是Android中有用的用户界面元素,允许使用自然用户界面选择整数值。 SeekBar的一个示例是设备的亮度控制和音量控制。但是您是否知道SeekBar可以实现为Unlock Slide Bar?在本文中,我们想与您分享如何使用SeekBar实现Unlock Slide Bar。

SeekBar和ProgressBar之间的区别

SeekBar具有与ProgressBar相同的属性。但是唯一的区别是用户可以通过移动SeekBar中的滑块(拇指)来确定进度。要将SeekBar添加到布局(XML)文件中,可以使用元素。以下是解锁滑动条的示例。

解锁滑杆示例

哪里可以使用它?

  • 要解锁屏幕,请解锁活动,然后转到活动(我们在本文中讨论的内容)。
  • 使用类似的概念来构建游戏。
  • 在付款网关确认结帐。
  • 用于关闭警报。

下面的样本GIF给出得到什么我们将在本文中做的想法。请注意,我们将使用Kotlin语言实施此项目。

样本GIF

方法

请参考以下几点,为我们实现的应用程序定义模块:

  • 该应用程序有2个活动,MainActivity和MainActivity2,都具有各自的布局文件activity_main和activity_main2。
  • SeekBar存在于第一个活动中,即在activity_main文件中声明。
  • 我们已经对SeekBar进行了编程,以使用户在其滑动到最后时将其带到一个新的活动,即MainActivity2。否则,SeekBar将其进度设置为0并显示一条Toast消息。
  • 第一个活动中提供了textView,以实时显示SeekBar的进度。

要在Android中创建滑动条,请执行以下步骤:

步骤1:创建一个新项目

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

步骤2:使用activity_main.xml文件

现在转到代表应用程序UI的activity_main.xml文件。如图所示,创建一个SeekBar和TextView。以下是activity_main.xml文件的代码。

XML


  
    
    
    
    
  
    
    
  


Kotlin
import android.content.Intent
import android.os.Bundle
import android.widget.SeekBar
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // declare the textView from the layout file
        val tv = findViewById(R.id.tv)
  
        // declare the SeekBar from the layout file
        val sb = findViewById(R.id.sbb)
  
        // Action when SeekBar is used
        sb.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
  
  
            // Member Implementation (Required)
            // Keeps the track if touch was lifted off the SeekBar
            override fun onStopTrackingTouch(seekBar: SeekBar) {
  
                // If touch was lifted before the SeekBar progress was 100
                // Make a Toast message "Try Again" and set the progress to 0
                if (seekBar.progress < 100) {
                    Toast.makeText(applicationContext, "Try Again", Toast.LENGTH_SHORT).show()
                    seekBar.progress = 0
                }
            }
  
            // Member Implementation (Required)
            override fun onStartTrackingTouch(seekBar: SeekBar) {
  
                // Do anything or Nothing
  
            }
  
            // Member Implementation (Required)
            // Keeps the track of progress of the seekbar
            override fun onProgressChanged(
                seekBar: SeekBar, progress: Int,
                fromUser: Boolean
            ) {
                // Show the progress when progress was less than 100
                if (progress < 100) {
                    tv.text = "Progress : $progress"
                }
  
                // If the progress is 100, take the user to another activity
                // Via Intent
                if (progress == 100) {
                    startActivity(
                        Intent(
                            applicationContext,
                            MainActivity2::class.java
                        )
                    )
                }
            }
        })
    }
}


XML


  
    
    
  


Kotlin
// No Changes in this file
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
  
class MainActivity2 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)
    }
}


步骤3:使用MainActivity.kt文件

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

科特林

import android.content.Intent
import android.os.Bundle
import android.widget.SeekBar
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
  
        // declare the textView from the layout file
        val tv = findViewById(R.id.tv)
  
        // declare the SeekBar from the layout file
        val sb = findViewById(R.id.sbb)
  
        // Action when SeekBar is used
        sb.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
  
  
            // Member Implementation (Required)
            // Keeps the track if touch was lifted off the SeekBar
            override fun onStopTrackingTouch(seekBar: SeekBar) {
  
                // If touch was lifted before the SeekBar progress was 100
                // Make a Toast message "Try Again" and set the progress to 0
                if (seekBar.progress < 100) {
                    Toast.makeText(applicationContext, "Try Again", Toast.LENGTH_SHORT).show()
                    seekBar.progress = 0
                }
            }
  
            // Member Implementation (Required)
            override fun onStartTrackingTouch(seekBar: SeekBar) {
  
                // Do anything or Nothing
  
            }
  
            // Member Implementation (Required)
            // Keeps the track of progress of the seekbar
            override fun onProgressChanged(
                seekBar: SeekBar, progress: Int,
                fromUser: Boolean
            ) {
                // Show the progress when progress was less than 100
                if (progress < 100) {
                    tv.text = "Progress : $progress"
                }
  
                // If the progress is 100, take the user to another activity
                // Via Intent
                if (progress == 100) {
                    startActivity(
                        Intent(
                            applicationContext,
                            MainActivity2::class.java
                        )
                    )
                }
            }
        })
    }
}

步骤4:建立另一个活动

右键单击应用程序文件夹>新建>活动>空活动,使用布局文件创建另一个活动。并参考下面的代码。仅对activity_main2.xml文件进行了更改,而未对MainActivity2.kt文件进行了更改。以下是activity_main2.xmlMainActivity2.kt文件的核心。

XML格式



  
    
    
  

科特林

// No Changes in this file
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
  
class MainActivity2 : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main2)
    }
}

输出:在模拟器上运行

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