如何从 seekbar Android Kotlin 更改字符串的长度

How to change the length of the string from the seekbar Android Kotlin

我正在尝试根据搜索栏值的长度制作密码生成器。

例如,当搜索栏位于 12 时,密码生成器将显示 12 个随机字符。

该应用在本例中为 16 的固定长度下运行良好。 主要活动。

class MainActivity : AppCompatActivity() {

    private lateinit var _binding : ActivityMainBinding
    private var startPoint = 0
    private var endPoint = 0


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        _binding = ActivityMainBinding.inflate(layoutInflater)
        val view = _binding.root
        setContentView(view)

        _binding.apply {

            generate.setOnClickListener {
                val generatePassword = randomID()
                Password.text = generatePassword
            }

            seekbar.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener{
                override fun onProgressChanged(
                    seekBar: SeekBar?,
                    progress: Int,
                    fromUser: Boolean
                ) {
                    volume.text = progress.toString()
                }

                override fun onStartTrackingTouch(seekBar: SeekBar?) {
                    startPoint = seekBar?.progress ?: 0
                }

                override fun onStopTrackingTouch(seekBar: SeekBar?) {
                    endPoint = seekBar?.progress ?: 0
                }

            })
        }

    }

    private  fun randomID() : String = List(16)  {
      val strings = (('a'..'z') + ('A'..'Z') + ('0'..'9'))
        strings.random()
        }.joinToString("")
    }

尝试这样的事情

 override fun onStopTrackingTouch(seekBar: SeekBar?) {
               Password.text= randomID(seekBar?.progress ?: 0)
            }

 private  fun randomID(length : Int) : String = List(length)  {
      val strings = (('a'..'z') + ('A'..'Z') + ('0'..'9'))
        strings.random()
        }.joinToString("")
    }