我目前正在制作一个包含“图像按钮”的应用。但是,单击图像按钮后,它不会触发动作,但是当您单击图像按钮两次时,它将启动Intent。
以下是activity_main.xml的代码
<FrameLayout 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:background="#0099cc"
tools:context=".FullscreenActivity"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/fullscreen_content_controls"></LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
android:scaleType="centerCrop"
android:id="@+id/fullscreen_content"
android:src="@drawable/mainscreen"/>
<ImageButton
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/gallerylogo2"
android:scaleType="fitCenter"
android:background="#00ffffff"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:foregroundGravity="center_horizontal"
android:id="@+id/gallerybutton"
android:focusableInTouchMode="true"
android:focusable="true"/>
以下是意图的代码:
btnGallery = (ImageButton) findViewById(R.id.gallerybutton);
btnGallery.setOnClickListener(goToGallery);
View.OnClickListener goToGallery = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setType("*/*");
startActivity(intent);
/*Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent , RESULT_GALLERY );*/
}
};
似乎是什么问题?
最佳答案
使用import android.view.View.OnClickListener;
并改用这段代码
OnClickListener goToGallery = new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setType("*/*");
startActivity(intent);
/*Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent , RESULT_GALLERY );*/
}
};