我想在 Kotlin 中自定義新按鈕時遇到問題 (I have a problem when I want to customize my new button in Kotlin)


問題描述

我想在 Kotlin 中自定義新按鈕時遇到問題 (I have a problem when I want to customize my new button in Kotlin)

當我在 Kotlin 中創建 myButton 時,我的代碼中有此錯誤。

在此處輸入圖片描述


參考解法

方法 1:

Button is a Function, not a class extended. Just make an function in MainActivity

For example

    class MainActivity : AppCompatActivity(){

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

        MyButton()  // this is how to make button inside function

    }

    private fun MyButton(){

     // you can put your button listener here
     // example :

     mMyBtn.setOnClickListener {
            // and then put ur logic here
            Toast.makeText(this, "Button Clicked", Toast.LENGTH_SHORT).show()
        }

    }
}

or maybe try below

class MyButton : androidx.appcompat.widget.AppCompatButton {
    constructor(context: Context?) : super(context!!)
    constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context!!, attrs, defStyle)

    protected override fun onDraw(canvasObject: Canvas) {
        super.onDraw(canvasObject)
    }
}

let me know if this works for you :)

方法 2:

Firstly it a warning, not an error. Your code will still run fine. But the reason for this warning/suggestion is because AppCompatButton is part of androidx library also comes with backward support for some features , they are also tint aware meaning it allows dynamic and background tints.

From the documentation of AppCompatButton

A Button which supports compatible features on older versions of the platform, >including:

  • Allows dynamic tint of its background via the background tint methods in ViewCompat.
  • Allows setting of the background tint using R.attr.backgroundTint and R.attr.backgroundTintMode.
  • Allows setting of the font family using R.attr.fontFamily.
  • This will automatically be used when you use Button in your layouts and the top‑level activity / dialog is provided by appcompat. You should only need to manually use this class when writing custom views.

From above, When ever you use a Button, it automatically uses AppCompatButton, so you should take care of extending AppCompatButton in custom views to get most of it.

(by DevCodexfathurrahmanRajan Kali)

參考文件

  1. I have a problem when I want to customize my new button in Kotlin (CC BY‑SA 2.5/3.0/4.0)

#syntax-error #Android #Kotlin






相關問題

SQL Server 2008 - 嘗試編輯日期 - 語法錯誤? (SQL Server 2008 - Trying to edit dates - Syntax Error?)

將 Jquery.min 升級到 Jquery.1.9 時出錯 (Error Upgrade Jquery.min into Jquery.1.9)

SyntaxError:無效語法(打印功能) (SyntaxError: invalid syntax (print function))

錯誤:“if”之前的預期表達式 (error: expected expression before ‘if’)

導致 C2061 的用戶創建的標頭:語法錯誤:標識符“類名” (User-Created Header Causing C2061: Syntax Error : Identifier 'classname')

If 語句行收到語法錯誤 (Syntax Error received on If statement line)

語法錯誤:到達了意外的文件結尾。你有一個未關閉的#if (Syntax error: Unexpected end of file reached. You have an unclosed #if)

已經放 } 但錯誤仍然說 } 是預期的? (Already put } but the error still says that } is expected?)

為什麼 Java 不允許在這裡使用三元運算符? (Why doesn't Java allow the use of a ternary operator here?)

我在繪製向量時遇到語法錯誤 (I'm encountering a syntax error while plotting vectors)

我想在 Kotlin 中自定義新按鈕時遇到問題 (I have a problem when I want to customize my new button in Kotlin)

為什麼帶註釋的變量不能是全局的? (Why can't an annotated variable be global?)







留言討論