简体   繁体   English

评级栏中的星级| kotlin

[英]rate stars in rating bar | kotlin

i'm new in kotlin and i need help,.我是 kotlin 的新手,我需要帮助。 i have list of 5 stars on the line(like rating bar) and want?我有 5 星列表(如评分栏),想要吗? for example when you clicked 3d star previous two change color?例如,当您单击 3d 星前两个更改颜色时? how can i write it with loop pragmaticaly?我怎样才能用循环写它? with if/else?如果/否则? i hope you help me!我希望你能帮助我! thanks!!谢谢!!

task:任务:

    // Input - get elements or number or other data

    //     list of stars (items and index of items)
    //     rating number (x/5) (number of clicked star)
    //
    // Loops - FOR - iterate over elements
    // Conditions - IF/ELSE - what to do with element

    // Action:
    //    loop over stars (star & indexOfStar)
    //    set color: (yellow for start behind chosen one including chosen one (rating number))

have this code:有这个代码:

 val star1: ImageView = binding.star1
        binding.star1.setOnClickListener {
            Log.d("Star 1","Rate 1")
            star1.setImageDrawable(ContextCompat.getDrawable(star1.context, com.example.godelreviews.R.drawable.star2))}

        val star2: ImageView = binding.star2
        binding.star2.setOnClickListener {
            Log.d("Star 2","Rate 2")
            star2.setImageDrawable(ContextCompat.getDrawable(star1.context, com.example.godelreviews.R.drawable.star2))}

        val star3: ImageView = binding.star3
        binding.star3.setOnClickListener {
            Log.d("Star 3","Rate 3")
            star3.setImageDrawable(ContextCompat.getDrawable(star1.context, com.example.godelreviews.R.drawable.star2))}

        val star4: ImageView = binding.star4
        binding.star4.setOnClickListener {
            Log.d("Star 4","Rate 4")
            star4.setImageDrawable(ContextCompat.getDrawable(star1.context, com.example.godelreviews.R.drawable.star2))}

        val star5: ImageView = binding.star5
        binding.star5.setOnClickListener {
            Log.d("Star 5","Rate 5")
            star5.setImageDrawable(ContextCompat.getDrawable(star1.context, com.example.godelreviews.R.drawable.star2))}

        return binding.root
    }

    private fun onRatingChanged(rating: Int){
        Log.d("AddPage", "onRatingChanged: $rating/5")
    }

    private fun getStarRating(): Int{
        TODO("Implement")
    }
}

now it looks like this:现在看起来像这样:

仅突出显示第三颗星星的图像

but i want mark others stars like this:但我想像这样标记其他明星:

突出显示前三个的星星的图像

Since this looks like homework I'm not going to tell you how to do it, but the instructions you posted are really helpful!因为这看起来像家庭作业,我不会告诉你怎么做,但你发布的说明真的很有帮助!

Action:行动:

  • loop over stars (star & indexOfStar)循环星星(star & indexOfStar)
  • set color: (yellow for start behind chosen one including chosen one (rating number))设置颜色:(黄色表示选择一个后面的开始,包括选择一个(评级编号))

It's saying you need to loop over all the stars.这就是说你需要遍历所有的星星。 If the current star is the chosen one, or it's before the chosen one, you need to set it to yellow.如果当前星是被选中的,或者在被选中的之前,则需要将其设置为黄色。 Otherwise you need to set it to grey or whatever.否则,您需要将其设置为灰色或其他。

You need to do this whenever the currently selected star changes - so it makes sense to put this loop in a function you can call whenever that happens.每当当前选定的星发生变化时,您都需要执行此操作 - 因此,将此循环放在 function 中是有意义的,您可以在发生这种情况时调用。 That way, all your click listeners have to do is set the current star rating, and call the function that updates the display.这样,您的点击监听器所要做的就是设置当前星级,并调用更新显示的 function。

You already have a function declared that looks a lot like this:你已经有一个 function 声明,看起来很像这样:

private fun onRatingChanged(rating: Int){
    ...
}

It's called onRatingChanged and takes a rating value.它被称为onRatingChanged并采用rating值。 How about you make that store the rating (if necessary), and then loop over your star ImageView s, updating each to display the appropriate star?您如何对该存储进行评级(如有必要),然后遍历您的星ImageView ,更新每个星号以显示适当的星号? Then your click listeners can just call that with the appropriate rating value.然后,您的点击侦听器可以使用适当的评级值来调用它。

Modify your code like this, It may work for you -像这样修改您的代码,它可能对您有用-

    binding.star1.setOnClickListener {
        Log.d("Star 2","Rate 2")
        binding.star1.setImageDrawable(ContextCompat.getDrawable(binding.star1.context, com.example.godelreviews.R.drawable.star2))} 
  
    binding.star2.setOnClickListener {
        Log.d("Star 2","Rate 2")
        binding.star1.setImageDrawable(ContextCompat.getDrawable(binding.star1.context, com.example.godelreviews.R.drawable.star2))
        binding.star2.setImageDrawable(ContextCompat.getDrawable(binding.star1.context, com.example.godelreviews.R.drawable.star2))}
    
    binding.star3.setOnClickListener {
        Log.d("Star 3","Rate 3")
        binding.star1.setImageDrawable(ContextCompat.getDrawable(binding.star1.context, com.example.godelreviews.R.drawable.star2))
        binding.star2.setImageDrawable(ContextCompat.getDrawable(binding.star1.context, com.example.godelreviews.R.drawable.star2))
        binding.star3.setImageDrawable(ContextCompat.getDrawable(binding.star1.context, com.example.godelreviews.R.drawable.star2))}
    
    binding.star4.setOnClickListener {
        Log.d("Star 4","Rate 4")
        binding.star1.setImageDrawable(ContextCompat.getDrawable(binding.star1.context, com.example.godelreviews.R.drawable.star2))
        binding.star2.setImageDrawable(ContextCompat.getDrawable(binding.star1.context, com.example.godelreviews.R.drawable.star2))
        binding.star3.setImageDrawable(ContextCompat.getDrawable(binding.star1.context, com.example.godelreviews.R.drawable.star2))
        binding.star4.setImageDrawable(ContextCompat.getDrawable(binding.star1.context, com.example.godelreviews.R.drawable.star2))}


    binding.star5.setOnClickListener {
        Log.d("Star 5","Rate 5")
        binding.star1.setImageDrawable(ContextCompat.getDrawable(binding.star1.context, com.example.godelreviews.R.drawable.star2))
        binding.star2.setImageDrawable(ContextCompat.getDrawable(binding.star1.context, com.example.godelreviews.R.drawable.star2))
        binding.star3.setImageDrawable(ContextCompat.getDrawable(binding.star1.context, com.example.godelreviews.R.drawable.star2))
        binding.star4.setImageDrawable(ContextCompat.getDrawable(binding.star1.context, com.example.godelreviews.R.drawable.star2))
        binding.star5.setImageDrawable(ContextCompat.getDrawable(binding.star1.context, com.example.godelreviews.R.drawable.star2))}

    return binding.root
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM