Java 图像按钮不可单击-AndroidStudio
我在Android应用程序上有4个ImageButton的声明,但到目前为止,它们不可单击,这是我的类:Java 图像按钮不可单击-AndroidStudio,java,android,android-layout,android-studio,Java,Android,Android Layout,Android Studio,我在Android应用程序上有4个ImageButton的声明,但到目前为止,它们不可单击,这是我的类: public class WelcomeScreen extends Activity { ImageButton completeprofile; ImageButton gotoportfolio; ImageButton findfriends; ImageButton readnews; public void onCreate(Bundle savedInstanceState
public class WelcomeScreen extends Activity {
ImageButton completeprofile;
ImageButton gotoportfolio;
ImageButton findfriends;
ImageButton readnews;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_activity);
completeprofile = (ImageButton) findViewById(R.id.completeprofile);
gotoportfolio = (ImageButton) findViewById(R.id.gotoportfolio);
findfriends = (ImageButton) findViewById(R.id.findfriends);
readnews = (ImageButton) findViewById(R.id.readnews);
completeprofile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(WelcomeScreen.this, ProfileMember.class);
startActivity(i);
}
});
gotoportfolio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(WelcomeScreen.this, PortfolioMember.class);
startActivity(i);
}
});
findfriends.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(WelcomeScreen.this, MainActivity.class);
startActivity(i);
}
});
readnews.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(WelcomeScreen.this, WebActivity.class);
startActivity(i);
}
});
} }
这是我的布局:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="70dp"
android:layout_marginLeft="105dp">
<ImageButton
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/completeprofile"
android:background="@drawable/completeprofile"
android:layout_marginLeft="75dp"
android:clickable="true" />
<ImageButton
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/gotoportfolio"
android:background="@drawable/gotoportfolio"
android:layout_marginLeft="65dp"
android:clickable="true" />
<ImageButton
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/findfriends"
android:background="@drawable/findfriends"
android:layout_marginLeft="65dp"
android:clickable="true" />
<ImageButton
android:layout_width="55dp"
android:layout_height="55dp"
android:id="@+id/readnews"
android:background="@drawable/readnews"
android:layout_marginLeft="65dp"
android:clickable="true" />
</LinearLayout>
它们显示得非常完美,但到目前为止,我无法单击其中任何一个,没有stacktrace错误,对此我感到非常困惑=/
有人能解释一下吗
提前谢谢 您正在使用PNG图像作为背景。单击按钮时,您应该使用
android:src
属性而不是android:background
来获取触摸反馈。如果要更改背景,必须使用XML可绘制选择器。请看这项工作对我来说很好
在Xml中使用它
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:layout_marginBottom="48dp"
android:onClick="AddInfo"
android:background="@mipmap/ea_logo"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
转到Build>Clean Project并重试。常规按钮有效吗?是的,我没有清理项目,因为我添加了它们,让我试试@Girubai建议的,还不起作用的:(我猜它们是纯PNG图像。您应该使用
android:src
属性,而不是android:background
。如果您想更改背景,必须使用XML可绘制选择器,请参阅文档。)
public void AddInfo(View view) {
///// Use Your Own code /////////
}