欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android實現(xiàn)點擊按鈕控制圖片切換

 更新時間:2021年01月20日 10:01:37   作者:Jedi Hongbin  
這篇文章主要為大家詳細介紹了android實現(xiàn)點擊按鈕控制圖片切換,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了android實現(xiàn)點擊按鈕控制圖片切換的具體代碼,供大家參考,具體內(nèi)容如下

代碼:

class MainActivity : AppCompatActivity() {

 override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.activity_main)
  initView()
 }

 private fun initView() {
  val chooseGridFriendBtn: Button = findViewById(R.id.chooseGridFriend)
  chooseGridFriendBtn.setOnClickListener { chooseGirlFriend() }
 }
 // 選女友的方法實現(xiàn)
 private fun chooseGirlFriend() {
  val chooseGirlfriend = ChooseGirlfriend(5) // 只有5張圖
  val girlFriend = chooseGirlfriend.renderChoose()
  Toast.makeText(this, "" + girlFriend, Toast.LENGTH_SHORT).show()

  // 顯示對應圖片
  val girlFriendImageSource = when (girlFriend){
   1 -> R.drawable._0
   2 -> R.drawable._1
   3 -> R.drawable._2
   4 -> R.drawable._3
   5 -> R.drawable._4
   else -> R.drawable.dice
  }

  // 展示選中圖片
  val girlFriendImageView: ImageView = findViewById(R.id.imageView)
  girlFriendImageView.setImageResource(girlFriendImageSource)

 }

}

/*
* 女友選擇器
* 隨機數(shù)選擇 默認1-6
* */

class ChooseGirlfriend(private val numDice: Int = 6) {
 fun renderChoose(): Int {
  return (1..numDice).random()
 }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
 android:orientation="vertical"
 tools:context=".MainActivity">
 
 <ImageView
  android:id="@+id/imageView"
  android:layout_width="215dp"
  android:layout_height="150dp"
  android:layout_gravity="center"
  android:layout_marginVertical="20dp"
  android:contentDescription="@string/dice_image"
  app:srcCompat="@drawable/dice" />

 <Button
  android:id="@+id/chooseGridFriend"
  android:layout_width="237dp"
  android:layout_height="wrap_content"
  android:layout_gravity="center"
  android:text="@string/choose_a_girlfriend" />
</LinearLayout>

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論