Android - 只需单击一下即可无法单击图像按钮

时间:2015-12-08 12:55:36

标签: java android android-imagebutton

我正在制作一个包含图像按钮的应用。但是,单击图像按钮时,它不会触发操作,但是当您单击图像按钮两次时,它将启动意图。

以下是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 );*/
    }
};

什么似乎是问题?

2 个答案:

答案 0 :(得分:1)

删除pause.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "pause", Toast.LENGTH_SHORT).show(); mediaPlayer.pause(); pause.setEnabled(false); play.setEnabled(true); } }); 并且

btnGallery.setOnClickListener(goToGallery);

答案 1 :(得分:1)

使用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 );*/
        }
    };