Android 为什么第一次单击ImageView不起作用?

Android 为什么第一次单击ImageView不起作用?,android,android-imageview,onclicklistener,Android,Android Imageview,Onclicklistener,我读过其他帖子。我仍然找不到解决办法 我试着把 android:focusableInTouchMode="false" android:focusable="false" 没用。 我包括 android:clickable="true" 没用 在Java代码中,我包含了 imgLeft.setOnFocusChangeListener(new View.OnFocusChangeListener() { public void onFocusChange(View v,

我读过其他帖子。我仍然找不到解决办法

我试着把

android:focusableInTouchMode="false"
android:focusable="false"
没用。 我包括

android:clickable="true"
没用

在Java代码中,我包含了

  imgLeft.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                v.performClick();
            }
        }
    });
除了onClickListener()

但它仍然不起作用

这里是我的粗略布局结构和图像视图的位置

  <RelativeLayout>
     <FrameLayout>
        <LinearLayout>
           <Relativelayout>
              <LineatLayout/>
                  <LinearLayout>
                      <LinearLayout>
                          <ImageView    <--- This is the I needed to click.
                            android:id="@+id/imgLeft"
                            android:layout_width="wrap_content"
                            android:layout_height="@dimen/title_img_width"
                            android:src="@drawable/left" 
                            />  
                      </LinearLayout>
                  </LinearLayout>
              </LinearLayout>
           </Relativelayout>
        </LinearLayout>
     </FrameLayout>
  </Relativelayout>


很简单。你可以试试这个

只需在您的图像视图中添加
android:onClick=“click”
(XML)

然后

已编辑

    YOUR_IMAGE_VIEW_OBJ.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Do your Staff

        }
     });

  • 为什么要使用imgLeft.setOnFocusChangeListener(新视图.OnFocusChangeListener())有什么问题reaaon@Dinesh是大图像还是类似按钮的东西?你试过了吗ImageButton@murugalonger我在其他stackoverfow帖子中看到了。我现在不能看。这里提到了使用setonFocusListener和onClickListener。@Pavan。它的小图像。不,我没有用过。如何与imageButton一起使用?我不确定是否有效,但您可以尝试,就像您必须用ImageButton替换ImageView一样。哦,您可以添加OnclickListenr设置OnFocusChangeListener或尝试ntelliJ Amiya建议的内容。尽管如此,我只能在第二次单击imgView。
     <RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rl_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg_image" >
    
    <FrameLayout
        android:id="@+id/layoutConfiguration"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="fitXY"
            android:src="@drawable/bg_image"
            android:visibility="gone" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="@dimen/menu_header_height" >
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:weightSum="1" >
    
                    <LinearLayout
                        android:id="@+id/ll_menu"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight=".2"
                        android:gravity="center"
                        android:orientation="vertical" >
    
                        <ImageView
                            android:layout_width="@dimen/menu_top_icon"
                            android:layout_height="match_parent"
                            android:layout_gravity="center"
                            android:padding="5dp"
                            android:src="@drawable/menu" />
                    </LinearLayout>
    
                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="0.68"
                        android:fontFamily="Oswald-Regular.ttf"
                        android:gravity="center"
                        android:text="Connect Bluetooths"
                        android:textColor="#fff"
                        android:textSize="@dimen/menu_tital_textsize" />
    
                    <!-- android:padding="@dimen/menu_tital_padding" -->
    
                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight=".2"
                        android:gravity="center"
                        android:padding="15dp"
                        android:text=""
                        android:textColor="#fff"
                        android:textSize="25dp"
                        android:textStyle="bold" />
                </LinearLayout>
            </RelativeLayout>
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal"
                    android:orientation="vertical" >
    
                    <LinearLayout
                        android:id="@+id/ll_sole"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/tital_margin_top"
                        android:gravity="center_horizontal"
                        android:orientation="horizontal"
                        android:weightSum="1" >
    
                        <LinearLayout
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.3"
                            android:orientation="vertical" >
    
                            <TextView
                                android:id="@+id/tv_leftBattery"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginRight="15dp"
                                android:fontFamily="Raleway-Regular.ttf"
                                android:gravity="center"
                                android:text="100%"
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                android:textColor="@android:color/holo_red_dark"
                                android:textSize="16sp"
                                android:visibility="gone" />
    
                            <ImageView
                                android:id="@+id/imgLeftBluetooth"      
                                android:layout_width="wrap_content"
                                android:layout_height="@dimen/title_img_width"
                                android:src="@drawable/left" />
    
                            <TextView
                                android:id="@+id/tv_leftBluetooth"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginRight="15dp"
                                android:fontFamily="Raleway-Regular.ttf"
                                android:gravity="center"
                                android:text="Not Connected"
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                android:textColor="@android:color/white"
                                android:textSize="14sp" />
                        </LinearLayout>
    
                        <LinearLayout
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.3"
                            android:orientation="vertical" >
    
                            <TextView
                                android:id="@+id/tv_rightBattery"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginRight="15dp"
                                android:fontFamily="Raleway-Regular.ttf"
                                android:gravity="center"
                                android:text="100%"
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                android:textColor="@android:color/holo_red_dark"
                                android:textSize="16sp"
                                android:visibility="gone" />
    
                            <ImageView
                                android:id="@+id/imgRightBluetooth"
                                android:layout_width="wrap_content"
                                android:layout_height="@dimen/title_img_width"
                                android:layout_marginTop="5dp"
                                android:src="@drawable/right" />
    
                            <TextView
                                android:id="@+id/tv_rightBluetooth"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginLeft="15dp"
                                android:fontFamily="Raleway-Regular.ttf"
                                android:gravity="center"
                                android:text="Not Connected"
                                android:textAppearance="?android:attr/textAppearanceMedium"
                                android:textColor="@android:color/white"
                                android:textSize="14sp" />
                        </LinearLayout>
                    </LinearLayout>
    
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal"
                        android:orientation="vertical"
                        android:paddingTop="@dimen/menu_tital_padding" >
    
                        <ToggleButton
                            android:id="@+id/toggleButton_connectBluetooth"
                            android:layout_width="@dimen/title_img_height"
                            android:layout_height="@dimen/title_text_height"
                            android:layout_centerHorizontal="true"
                            android:layout_centerVertical="true"
                            android:background="@drawable/toggle_selector"
                            android:checked="false"
                            android:padding="5dp"
                            android:text=""
                            android:textOff=""
                            android:textOn="" />
    
                        <TextView
                            android:id="@+id/tv_toggle_btn"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="center"
                            android:padding="7dp"
                            android:text="Connect Left Bluetooths Only"
                            android:textAppearance="?android:attr/textAppearanceSmall"
                            android:textColor="@android:color/white" />
                    </LinearLayout>
                </LinearLayout>
    
                <FrameLayout
                    android:id="@+id/fl_balloon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="-15dp"
                    android:gravity="center"
                    android:visibility="gone" >
    
                    <ImageView
                        android:id="@+id/imageView2"
                        android:layout_width="@dimen/balloon_width"
                        android:layout_height="@dimen/balloon_height"
                        android:layout_gravity="center"
                        android:scaleType="fitXY"
                        android:src="@drawable/balloon" />
    
                    <TextView
                        android:id="@+id/textView2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:layout_marginLeft="7dp"
                        android:fontFamily="Oswald-Regular.ttf"
                        android:gravity="center"
                        android:text="Pair"
                        android:textColor="@android:color/white" />
                </FrameLayout>
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentTop="true"
                    android:layout_centerHorizontal="true"
                    android:fontFamily="Oswald-Regular.ttf"
                    android:text="Configure Bluetooths"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:textSize="@dimen/menu_tital_textsize"
                    android:visibility="gone" />
    
                <Button
                    android:id="@+id/btn_next"
                    android:layout_width="wrap_content"
                    android:layout_height="@dimen/menu_top_icon"
                    android:layout_alignParentBottom="true"
                    android:layout_centerHorizontal="true"
                    android:layout_marginBottom="29dp"
                    android:background="@drawable/button_box"
                    android:padding="3dp"
                    android:text="Continue"
                    android:textColor="@android:color/white"
                    android:textSize="15dp"
                    android:textStyle="bold" />
            </RelativeLayout>
        </LinearLayout>
    </FrameLayout>
     </RelativeLayout>
    
     public void clicking(View v)
      {
    
        Toast.makeText(v.getContext(),"Clicking On Image",Toast.LENGTH_LONG).show();
      }
    
        YOUR_IMAGE_VIEW_OBJ.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Do your Staff
    
            }
         });