android實(shí)現(xiàn)點(diǎn)擊按鈕控制圖片切換
本文實(shí)例為大家分享了android實(shí)現(xiàn)點(diǎ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() }
}
// 選女友的方法實(shí)現(xiàn)
private fun chooseGirlFriend() {
val chooseGirlfriend = ChooseGirlfriend(5) // 只有5張圖
val girlFriend = chooseGirlfriend.renderChoose()
Toast.makeText(this, "" + girlFriend, Toast.LENGTH_SHORT).show()
// 顯示對(duì)應(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)
}
}
/*
* 女友選擇器
* 隨機(jī)數(shù)選擇 默認(rèn)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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android點(diǎn)擊事件之多點(diǎn)觸摸與手勢(shì)識(shí)別的實(shí)現(xiàn)
- Android開(kāi)發(fā)返回鍵明暗點(diǎn)擊效果的實(shí)例代碼
- Android實(shí)現(xiàn)自動(dòng)點(diǎn)擊無(wú)障礙服務(wù)功能的實(shí)例代碼
- Android自定義SeekBar實(shí)現(xiàn)滑動(dòng)驗(yàn)證且不可點(diǎn)擊
- Android實(shí)現(xiàn)點(diǎn)擊某個(gè)按鈕指定位置彈出布局
- Android實(shí)現(xiàn)圖片點(diǎn)擊放大
- android實(shí)現(xiàn)點(diǎn)擊圖片全屏展示效果
- Android Button按鈕點(diǎn)擊背景和文字變化操作
- Android Studio finish()方法的使用與解決app點(diǎn)擊“返回”(直接退出)
- 關(guān)于android連續(xù)點(diǎn)擊出現(xiàn)多個(gè)Activity界面的解決方法
- Android實(shí)現(xiàn)WebView點(diǎn)擊攔截跳轉(zhuǎn)原生
- Android擴(kuò)大View點(diǎn)擊范圍的方法
相關(guān)文章
Android實(shí)現(xiàn)炫酷的網(wǎng)絡(luò)直播彈幕功能
這篇文章主要為大家詳細(xì)介紹了Android仿網(wǎng)絡(luò)直播彈幕功能的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
android實(shí)現(xiàn)用戶(hù)體驗(yàn)超棒的微信WebView進(jìn)度條
本篇文章主要介紹了android實(shí)現(xiàn)用戶(hù)體驗(yàn)超棒的微信WebView進(jìn)度條,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
Flutter?Widget開(kāi)發(fā)之Focus組件圖文詳解
這篇文章主要為大家介紹了Flutter?Widget開(kāi)發(fā)之Focus組件圖文詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android studio配置國(guó)內(nèi)鏡像源的實(shí)現(xiàn)
這篇文章主要介紹了Android studio配置國(guó)內(nèi)鏡像源的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Android開(kāi)發(fā)中使用mms模塊收發(fā)單卡和雙卡短信的教程
這篇文章主要介紹了Android開(kāi)發(fā)中使用mms模塊收發(fā)單卡和雙卡短信的教程,文中舉了MOTO XT800手機(jī)(估計(jì)已經(jīng)落伍很久了--)的例子來(lái)說(shuō)明如何解決雙卡雙待時(shí)的短信異常問(wèn)題,需要的朋友可以參考下2016-02-02
Android編程實(shí)現(xiàn)文字倒影效果的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)文字倒影效果的方法,涉及Android布局與圖形繪制相關(guān)操作技巧,需要的朋友可以參考下2017-03-03
Android實(shí)現(xiàn)延遲的幾種方法小結(jié)
這篇文章主要介紹了Android實(shí)現(xiàn)延遲的幾種方法,結(jié)合實(shí)例總結(jié)了Android實(shí)現(xiàn)延遲的幾種常見(jiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-02-02
Qt for Android開(kāi)發(fā)實(shí)例教程
這篇文章主要介紹了Qt for Android開(kāi)發(fā)的方法,具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2014-08-08
Android判斷某個(gè)權(quán)限是否開(kāi)啟的方法
今天小編就為大家分享一篇Android判斷某個(gè)權(quán)限是否開(kāi)啟的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07

