在Android中创建自定义按钮 [英] Creating custom button class in Android

查看:158
本文介绍了在Android中创建自定义按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建自定义按钮为我的Andr​​oid应用

I'm trying to create custom button class for my android app

public class TicTacButton extends Button 

我已经设置 TicTacButton 和创建的自定义方法和属性里面的所有构造函数。在我的主要活动中,我试图初始化按钮为

I've set all the constructors inside the TicTacButton and created custom methods and properties. In my main activity, I've tried to initialize the Buttons as

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

我收到一个

java.castClassException。 android.widget.Button不能转换为
  com.example.tictactoetitan.TicTacButton

java.castClassException. android.widget.Button cannot be cast to com.example.tictactoetitan.TicTacButton

我试图改变我的XML文件

I tried changing my xml file as

<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文件中的完整包名固定它。

Using the full package name in the XML file fixed it.

<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" />

这篇关于在Android中创建自定义按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆