为什么调用自定义Button控件不能正常显示窗口

Taotesea 2014-01-14 05:46:30

写了一个继承Button的类
public class MyButton extends Button {
public MyButton(Context context) {
super(context);
}

public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MyButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
}

//这里类进行调用
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyButton my=(MyButton)findViewById(R.id.buttonOK);//不能正常显示窗口
//Button my=(Button)findViewById(R.id.buttonOK);//可以能正常显示窗口
}
}
...全文
212 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
Taotesea 2014-01-15
  • 打赏
  • 举报
回复
谢谢了.........
triplesky001 2014-01-15
  • 打赏
  • 举报
回复
<com.xxx.xxx.MyButton android:id="@+id/buttonOK" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="62dp" android:text="Button" /> </RelativeLayout>
MR__P 2014-01-15
  • 打赏
  • 举报
回复
#3很详细,采纳就行
niekangshibendan 2014-01-15
  • 打赏
  • 举报
回复
引用 2 楼 taotesea 的回复:
谢谢ncepu307你的回答,但是在Button中没有找到你要求改的地方,我将XML代码显示,请教在现有的代码中如何写,谢谢:) <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/buttonOK" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="62dp" android:text="Button" /> </RelativeLayout>
已经很明显了,XML中引用Button是系统自己的Button,自定义的必须给出完整的路径,也就是包名.MyButton
sziicool 2014-01-15
  • 打赏
  • 举报
回复
正解正解!都是正解
SayHehe酱 2014-01-15
  • 打赏
  • 举报
回复
你在layout布局文件中button要用你自定义的button的包名+类名
Taotesea 2014-01-14
  • 打赏
  • 举报
回复
谢谢ncepu307你的回答,但是在Button中没有找到你要求改的地方,我将XML代码显示,请教在现有的代码中如何写,谢谢:) <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/buttonOK" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="62dp" android:text="Button" /> </RelativeLayout>
依然绿茶 2014-01-14
  • 打赏
  • 举报
回复
你的activity_main.xml中使用Button的地方改成com.xxx.xxx.MyButton(就是你的 包名.MyButton), 那么MyButton my=(MyButton)findViewById(R.id.buttonOK);这句话是可以正常显示。

80,362

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧