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()
// 顯示對應(yīng)圖片
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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android點擊事件之多點觸摸與手勢識別的實現(xiàn)
- Android開發(fā)返回鍵明暗點擊效果的實例代碼
- Android實現(xiàn)自動點擊無障礙服務(wù)功能的實例代碼
- Android自定義SeekBar實現(xiàn)滑動驗證且不可點擊
- Android實現(xiàn)點擊某個按鈕指定位置彈出布局
- Android實現(xiàn)圖片點擊放大
- android實現(xiàn)點擊圖片全屏展示效果
- Android Button按鈕點擊背景和文字變化操作
- Android Studio finish()方法的使用與解決app點擊“返回”(直接退出)
- 關(guān)于android連續(xù)點擊出現(xiàn)多個Activity界面的解決方法
- Android實現(xiàn)WebView點擊攔截跳轉(zhuǎn)原生
- Android擴大View點擊范圍的方法
相關(guān)文章
Android實現(xiàn)炫酷的網(wǎng)絡(luò)直播彈幕功能
這篇文章主要為大家詳細介紹了Android仿網(wǎng)絡(luò)直播彈幕功能的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
android實現(xiàn)用戶體驗超棒的微信WebView進度條
本篇文章主要介紹了android實現(xiàn)用戶體驗超棒的微信WebView進度條,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03
Flutter?Widget開發(fā)之Focus組件圖文詳解
這篇文章主要為大家介紹了Flutter?Widget開發(fā)之Focus組件圖文詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
Android studio配置國內(nèi)鏡像源的實現(xiàn)
這篇文章主要介紹了Android studio配置國內(nèi)鏡像源的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Android開發(fā)中使用mms模塊收發(fā)單卡和雙卡短信的教程
這篇文章主要介紹了Android開發(fā)中使用mms模塊收發(fā)單卡和雙卡短信的教程,文中舉了MOTO XT800手機(估計已經(jīng)落伍很久了--)的例子來說明如何解決雙卡雙待時的短信異常問題,需要的朋友可以參考下2016-02-02

