zoukankan      html  css  js  c++  java
  • Android中遇到加载View以及view里面有点击控件时,怎么避免焦点

      这个文章纯属是从自己多次摔倒的经历中让我不得不整理起来。其实,感觉每次都不知道怎么解决这个问题,但是,下次遇到的时候还是有被卡住了,我相信这里还有很多人跟我一样。

      其实在Android中,这个问题是很常见的,但是,还是有很多没有的到最根本的解决。

       我不是一个理论知识家,只是记下我觉得需要记下的东西,当然,这里我肯定希望能帮到更多的人。谢谢

    EG:

    这里有一个xml写的是需要填充内容的listview或者gridview(A),然后,肯定这里还会有另外一个xml用来布局listview里面的样式。我一般以cell结尾(B)。

    这里,你会发现如果在B里面我们有获得焦点的控件的时候,对于A里面的控件在实现onitemclicklistener的时候,根本没有执行。

    其实,是因为他的点击事件已经被他自己布局里面的控件给获取了,所以,我们应该在B的大布局里面加上:  android:descendantFocusability="blocksDescendants",

    beforeDescendants:viewgroup会优先其子类控件而获取到焦点   
    afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点   
    blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点   

    然后,在不想他抢走焦点的控件上面写好这两句就okay:

    android:clickable="true"
    android:focusable="false"

    其它的跟Listview同风格的照做就好:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:lhl="http://schemas.android.com/apk/res/com.childhos.childhos"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/layout_game_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="blocksDescendants"
        android:orientation="horizontal" >
    
        <com.lhl.view.RoundImageView
            android:id="@+id/image_game"
            android:layout_width="@dimen/btn_one_height"
            android:layout_height="@dimen/btn_one_height"
            android:layout_gravity="center"
            android:layout_margin="10dip"
            android:src="@drawable/image_game_one"
            lhl:borderRadius="15dp"
            lhl:type="round" />
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical" >
    
            <TextView
                android:id="@+id/text_name"
                style="@style/TextStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000"
                android:textSize="@dimen/text_four" />
    
            <TextView
                android:id="@+id/text_down_time"
                style="@style/TextStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000"
                android:textSize="@dimen/text_four" />
    
            <TextView
                android:id="@+id/text_app_size"
                style="@style/TextStyle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000"
                android:textSize="@dimen/text_four" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right|center_vertical"
            android:layout_weight="1"
            android:gravity="right|center_vertical"
            android:orientation="vertical" >
    
            <TextView
                android:id="@+id/btn_install"
                android:layout_width="@dimen/btn_one_height"
                android:layout_height="30dip"
                android:layout_gravity="right|center_vertical"
                android:layout_margin="10dip"
                android:background="@drawable/border_sel"
                android:clickable="true"
                android:focusable="false"
                android:gravity="center"
                android:text="安装"
                android:textColor="#398de3"
                android:textSize="@dimen/text_three" />
        </LinearLayout>
    
    </LinearLayout>

    谢谢!红牛,加油!

  • 相关阅读:
    sqlserver2008 查看数据库自带的索引建议
    DataSnap服务器生成的ID自动更新到客户端
    outlook 插件:导出rss的link地址
    eclipse 升级note
    合并百度影音的离线数据 with python 2.2 bdv格式的更新
    windows ubuntu bcdeditor
    合并百度影音的离线数据 with python 2.1 bdv格式的更新
    合并百度影音的离线数据 with python 第二版 基于yield
    php密码加密(密码散列)
    PHP (超文本预处理器)
  • 原文地址:https://www.cnblogs.com/Catherine-Brain/p/4381013.html
Copyright © 2011-2022 走看看