2024-02-26

【Kotlin】disable/enable ImageButton

參考資料 ----


雖然在 layout.xml 中定義 enabled="false"AndroidStudio 並不會報錯,但在 app 執行時並沒有產生作用;官網的 ImageButton 參考頁也沒有找到 enabled 的屬性,似乎只能在 app 執行時設定。
  1.  
  2. ...
  3. ...
  4.  
  5. <ImageButton
  6. android:id="@+id/Button1"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content"
  9. android:src="@drawable/圖檔" 本次採用 向量圖檔(vector asset), 所以可在程式中改變圖檔的顏色
  10. android:background="@android:color/transparent" 指定按鈕的背景為透明
  11. android:enabled="true" /> 這個屬性在 app 執行時並沒有產生作用
  12.  


  1.  
  2.  
  3. ...
  4. ...
  5.  
  6. override fun onCreate(savedInstanceState: Bundle?) {
  7. super.onCreate(savedInstanceState)
  8. setContentView(R.layout.activity_main)
  9. var button1 = findViewById<ImageButton>(R.id.Button1)
  10. button1.isEnabled = false
  11. // 即使被 disabled 了, button1 所引用的圖檔顏色並沒有任何變化
  12. // 所以也要自行改變圖檔的顏色
  13. button1.setColorFilter(getColor(R.color.black))
  14. }
  15.  


沒有留言:

張貼留言