簡體   English   中英

在Android中創建自定義按鈕類

[英]Creating custom button class in Android

我正在嘗試為我的Android應用程序創建自定義按鈕類

public class TicTacButton extends Button 

我已經在TicTacButton設置了所有構造函數,並創建了自定義方法和屬性。 在我的主要活動中,我嘗試將Buttons初始化為

TicTacButton btn = (TicTacButton) findViewById(R.id.button1);

我正在

java.castClassException。 android.widget.Button無法轉換為com.example.tictactoetitan.TicTacButton

我嘗試將xml文件更改為

<TicTacButton
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button1"
        android:layout_toRightOf="@+id/button1" />

沒用

使用XML文件中的完整軟件包名稱進行修復。

<com.example.tictactoetitan.TicTacButton
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button1"
        android:layout_toRightOf="@+id/button1" />

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM