简体   繁体   English

在Android中创建自定义按钮类

[英]Creating custom button class in Android

I'm trying to create custom button class for my android app 我正在尝试为我的Android应用程序创建自定义按钮类

public class TicTacButton extends Button 

I've set all the constructors inside the TicTacButton and created custom methods and properties. 我已经在TicTacButton设置了所有构造函数,并创建了自定义方法和属性。 In my main activity, I've tried to initialize the Buttons as 在我的主要活动中,我尝试将Buttons初始化为

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

I'm getting a 我正在

java.castClassException. java.castClassException。 android.widget.Button cannot be cast to com.example.tictactoetitan.TicTacButton android.widget.Button无法转换为com.example.tictactoetitan.TicTacButton

I tried changing my xml file as 我尝试将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" />

It didn't work. 没用

Using the full package name in the XML file fixed it. 使用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