我的 Android 应用程序上有 4 个 ImageButton 的声明,但到目前为止它们不可点击,这是我的类:
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>
它们显示得很完美,但到目前为止我无法点击其中任何一个,没有堆栈跟踪错误,我对此很困惑=/
有人可以解释一下吗?
提前致谢!
最佳答案
您正在使用 PNG 图像作为背景。您应该使用 android:src
属性而不是 android:background
来获取单击按钮时的触摸反馈。如果要更改背景,则必须使用 XML 可绘制选择器。请参阅docs
关于java - ImageButton不可点击 - AndroidStudio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30950215/