Android 当指定任何可绘制按钮时,该按钮不起作用
发生了一件我以前从未遇到过的奇怪事情,我正在使用一个按钮,它通过ontouchlistener执行某些操作,在将任何可绘制内容分配给按钮之前,它工作正常,但当我将可绘制内容分配给它时,它停止执行它应该执行的功能Android 当指定任何可绘制按钮时,该按钮不起作用,android,eclipse,Android,Eclipse,发生了一件我以前从未遇到过的奇怪事情,我正在使用一个按钮,它通过ontouchlistener执行某些操作,在将任何可绘制内容分配给按钮之前,它工作正常,但当我将可绘制内容分配给它时,它停止执行它应该执行的功能 <Button android:id="@+id/button1" android:layout_width="120dp" android:layout_height="150dp" android:layout_c
<Button
android:id="@+id/button1"
android:layout_width="120dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/image"
android:src="@drawable/image" />
提前感谢您正在将背景和src添加为同一图像 这似乎是你的错误 生成按钮xml代码:
<Button
android:id="@+id/button1"
android:layout_width="120dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/image" />
希望这能解决您的问题在activity xml中将按钮更改为ImageView 替换
<Button
android:id="@+id/button1"
android:layout_width="120dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/image"
android:src="@drawable/image" />
}) 这个错误对我来说毫无意义,它以前从未发生过。但事实证明,当我在drawable中创建一个XML文件并为其分配按钮时,我就解决了问题。xml文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/thumb"/>
<item android:state_pressed="true" android:drawable="@drawable/thumb"/>
<item android:drawable="@drawable/thumb"/>
</selector>
奇怪但对我有用,我想和大家分享
感谢所有努力回答这个问题的人 不可能,但是我猜你只是希望它有一个被按下的动画,你为android:background和android:srcpost添加了相同的@drawable/image。据我所知,按钮没有android:src,它们只有背景。ImageView和ImageButton具有android:srcI已编辑我的答案。。谢谢你的帮助@tasomaniac
ImageView button1 = (ImageView)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//do your work here
}
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:drawable="@drawable/thumb"/>
<item android:state_pressed="true" android:drawable="@drawable/thumb"/>
<item android:drawable="@drawable/thumb"/>
</selector>