的onClick不会开枪的ImageView [英] onClick won't fire on ImageView

查看:123
本文介绍了的onClick不会开枪的ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在申请3-4活动,个个有一些事件侦听器的工作很好。
但是,只有在一个活动我根本无法获得事件处理工作。
我想从这个线程解决方案:HTTP://www.anddev.org/view-layout-resource-problems-f27/ontouch-not-called-t16578.html
它不为我工作。我试图手动设置为OnClickListeners从Java code,机器人ImageViews:从的onClick XML

看来,一些其他组件处理所有的事件,或我的活动不具有一定的权限来处理事件。
我应该把在AndroidMainfest.xml东西我活动了能够处理的事件?

希望有人有这个想法,我应该尝试一下,这里的code:

活动:

 包com.renegade.begining;进口android.app.Activity;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.os.Bundle;进口android.view.MotionEvent;
进口android.view.View;
进口android.widget.ImageView;
公共类NotesOnStaff延伸活动{
   @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.notes_on_staff);
   / * NoteView2注=(NoteView2)findViewById(R.id.note_red);
        位图BM = BitmapFactory.de codeResource(getResources(),R.drawable.note_red);
        note.setImage(BM);
     * /    }
    公共无效onClickEventBtn1(视图v){
      // TODO自动生成方法存根
       ImageView的keyboard_letter =(ImageView的)findViewById(R.id.keyboard_letters);
       keyboard_letter.setVisibility(View.INVISIBLE);
   }    公共无效onClickEventBtn2(视图v){
      // TODO自动生成方法存根
       ImageView的keyboard_letter =(ImageView的)findViewById(R.id.keyboard_letters);
       keyboard_letter.setVisibility(View.VISIBLE);
   }    @覆盖
    公共布尔onTouchEvent(MotionEvent事件){
       开关(event.getAction()){
      案例MotionEvent.ACTION_DOWN:
         ImageView的keyboard_letter =(ImageView的)findViewById(R.id.keyboard_letters);
           keyboard_letter.setVisibility(View.VISIBLE);
         返回true;
      案例MotionEvent.ACTION_UP:
        返回false; //别的东西
                默认:
                        返回false; //所有其​​他
      }
    }
}

布局:

 <?XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的
  的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
  机器人:ID =@ + ID / notes_on_staff_layout
  机器人:背景=@绘制/ background_ver
  机器人:layout_width =FILL_PARENT
  机器人:layout_height =FILL_PARENT>
            < ImageView的
              机器人:ID =@ + ID / notes_keyboard
           机器人:SRC =@绘制/键盘
           机器人:layout_height =WRAP_CONTENT
           机器人:layout_width =WRAP_CONTENT
           机器人:layout_alignParentBottom =真
             机器人:layout_centerHorizo​​ntal =真
           机器人:layout_marginBottom =20dip         机器人:可点击=真正的机器人:可聚焦=真/>      < ImageView的
           机器人:ID =@ + ID / btn_hide
           机器人:SRC =@绘制/ btnhide
           机器人:layout_alignParentLeft =真
           机器人:layout_marginTop =40dip
           机器人:layout_marginLeft =10dip
           机器人:layout_height =WRAP_CONTENT
           机器人:layout_width =WRAP_CONTENT
            安卓的onClick =onClickEventBtn1
         机器人:可点击=真正的机器人:可聚焦=真/>      < ImageView的
         机器人:ID =@ + ID / btn_show
          机器人:layout_alignBottom =@ ID / btn_hide
          机器人:layout_toRightOf =@ ID / btn_hide
         机器人:SRC =@绘制/ btnshow
         机器人:layout_height =WRAP_CONTENT
         机器人:layout_width =WRAP_CONTENT
         安卓的onClick =onClickEventBtn2
          机器人:可点击=真正的机器人:可聚焦=真/>      < ImageView的           机器人:ID =@ + ID /参谋
           机器人:SRC =@绘制/参谋
           机器人:layout_below =@ ID / btn_hide
           机器人:layout_height =WRAP_CONTENT
           机器人:layout_width =WRAP_CONTENT
           机器人:layout_centerHorizo​​ntal =真
           机器人:layout_marginTop =0dip
           机器人:layout_above =@ ID / notes_keyboard
         />
      < com.renegade.begining.NoteView2
        机器人:ID =@ + ID / note_red
      机器人:layout_width =WRAP_CONTENT
       机器人:layout_height =WRAP_CONTENT
           />
      < ImageView的
         机器人:ID =@ + ID / keyboard_letters
          机器人:layout_alignLeft =@ + ID / notes_keyboard
          机器人:layout_alignBottom =@ + ID / notes_keyboard
          机器人:layout_marginBottom =10dip
          机器人:知名度=隐形
         机器人:SRC =@绘制/ keyboard_letters
         机器人:layout_height =WRAP_CONTENT
         机器人:layout_width =WRAP_CONTENT
          机器人:可点击=真正的机器人:可聚焦=真/>
< / RelativeLayout的>


解决方案

您的EventListener是吸收所有DOWN事件。试着让它返回的而不是真这样子视图得到一个机会,反应为好,因为我觉得对于的onClick 来消防适当子视图需要注册一个持续向下那么UP(只是一个UP是不够的,想想滑动手指,一键到另一个然后释放;也火灾因为这是用户想要取消指示。)

I have 3-4 activities in the application and all of them have some event listeners that work nicely. However only on one activity i simply can't get the event handling to work. I tried the solution from this thread:http://www.anddev.org/view-layout-resource-problems-f27/ontouch-not-called-t16578.html It doesn't work for me. I tried to manually set OnClickListeners for ImageViews from java code, android:onClick from XML.

It seems that some other component handles all the events, or my activity doesn't have some permission to handle events. Should I put something in the AndroidMainfest.xml for my activity that enables handling events?

Hope someone has the idea what should i try, here's the code:

Activity:

package com.renegade.begining;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;

import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;


public class NotesOnStaff extends Activity{
   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.notes_on_staff);


   /*     NoteView2 note=(NoteView2)findViewById(R.id.note_red);


        Bitmap bm=BitmapFactory.decodeResource(getResources(), R.drawable.note_red);
        note.setImage(bm);
     */   



    }


    public void onClickEventBtn1(View v) {
      // TODO Auto-generated method stub
       ImageView keyboard_letter=(ImageView)findViewById(R.id.keyboard_letters);
       keyboard_letter.setVisibility(View.INVISIBLE);
   }

    public void onClickEventBtn2(View v) {
      // TODO Auto-generated method stub
       ImageView keyboard_letter=(ImageView)findViewById(R.id.keyboard_letters);
       keyboard_letter.setVisibility(View.VISIBLE);
   }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
       switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
         ImageView keyboard_letter=(ImageView)findViewById(R.id.keyboard_letters);
           keyboard_letter.setVisibility(View.VISIBLE);
         return true;
      case MotionEvent.ACTION_UP:
        return false; // something else
                default:
                        return false;// all others
      }
    }


}

Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/notes_on_staff_layout"
  android:background="@drawable/background_ver"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">


            <ImageView
              android:id="@+id/notes_keyboard"
           android:src="@drawable/keyboard"
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"
           android:layout_alignParentBottom="true"
             android:layout_centerHorizontal="true"
           android:layout_marginBottom="20dip"   

         android:clickable="true" android:focusable="true"/>

      <ImageView
           android:id="@+id/btn_hide"
           android:src="@drawable/btnhide"
           android:layout_alignParentLeft="true"
           android:layout_marginTop="40dip"
           android:layout_marginLeft="10dip"
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"
            android:onClick="onClickEventBtn1"
         android:clickable="true" android:focusable="true"/>

      <ImageView 
         android:id="@+id/btn_show"
          android:layout_alignBottom="@id/btn_hide"
          android:layout_toRightOf="@id/btn_hide"
         android:src="@drawable/btnshow" 
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content"
         android:onClick="onClickEventBtn2"
          android:clickable="true" android:focusable="true"/>   



      <ImageView

           android:id="@+id/staff"
           android:src="@drawable/staff"
           android:layout_below="@id/btn_hide"
           android:layout_height="wrap_content"
           android:layout_width="wrap_content"
           android:layout_centerHorizontal="true"
           android:layout_marginTop="0dip"
           android:layout_above="@id/notes_keyboard"


         />   


      <com.renegade.begining.NoteView2 
        android:id="@+id/note_red"
      android:layout_width="wrap_content"
       android:layout_height="wrap_content"


           />      


      <ImageView 
         android:id="@+id/keyboard_letters"
          android:layout_alignLeft="@+id/notes_keyboard"
          android:layout_alignBottom="@+id/notes_keyboard"
          android:layout_marginBottom="10dip"
          android:visibility="invisible"
         android:src="@drawable/keyboard_letters" 
         android:layout_height="wrap_content" 
         android:layout_width="wrap_content"
          android:clickable="true" android:focusable="true"/>   


</RelativeLayout>

解决方案

Your EventListener is absorbing all DOWN events. Try having it return false instead of true so subviews get a chance to react as well, as I think for onClick to fire properly the child view needs to register a continuous DOWN then an UP (just an UP isn't enough; think about sliding your finger off of a button onto another one then releasing; neither fires as that's an indication that the user wants to cancel.)

这篇关于的onClick不会开枪的ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆