Android — кнопки изображений нельзя нажимать одним щелчком мыши

В настоящее время я делаю приложение, содержащее кнопки изображений. Однако при нажатии кнопки изображения оно не запускает действие, но когда вы дважды нажимаете кнопку изображения, оно запускает 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"/>

Below is the code for the intent:

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

В чем проблема?


person giroxasmnl    schedule 08.12.2015    source источник
comment
какой метод используется в вашем слушателе?   -  person bofredo    schedule 08.12.2015
comment
@bolfredo View.onClickListener   -  person giroxasmnl    schedule 08.12.2015
comment
android:largeHeap=true, почему это имеет отношение к вопросу?   -  person noev    schedule 08.12.2015
comment
@noev, потому что я подозревал, что это все из-за графики моего приложения, и оно съедает много памяти, так как одно действие эквивалентно 57,072 МБ.   -  person giroxasmnl    schedule 08.12.2015
comment
ты использовал android:focusableInTouchMode   -  person IntelliJ Amiya    schedule 08.12.2015
comment
пожалуйста, опубликуйте свои коды   -  person IntelliJ Amiya    schedule 08.12.2015
comment
Я отредактировал свой пост и добавил коды @IntelliJAmiya   -  person giroxasmnl    schedule 08.12.2015
comment
неправильный порядок вашего goToGallery. Это вообще не должно работать   -  person bofredo    schedule 08.12.2015
comment
Как ты так сказал @bofredo?   -  person giroxasmnl    schedule 08.12.2015
comment
во 2-й строке вы используете переменную, объявленную в строке 3   -  person bofredo    schedule 08.12.2015


Ответы (2)


используйте импорт 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 );*/
        }
    };
person Ido Ben Shalom    schedule 08.12.2015

Удалить btnGallery.setOnClickListener(goToGallery); И

btnGallery.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        // Your staff

        Intent intent = new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);

        intent.setType("*/*");
        startActivity(intent);
    }

});
person IntelliJ Amiya    schedule 08.12.2015
comment
используя setOnClickListener - person IntelliJ Amiya; 08.12.2015
comment
stackoverflow.com/questions/20755322/ - person IntelliJ Amiya; 08.12.2015
comment
@ user5290675 проверь мой ответ - person IntelliJ Amiya; 08.12.2015
comment
что такое View.OnClickListener? - person IntelliJ Amiya; 08.12.2015
comment
@user5290675 очень странный случай - person IntelliJ Amiya; 08.12.2015