zoukankan      html  css  js  c++  java
  • Android中如何取消调转界面后EditText默认获取聚焦问题

    最近在做一个项目,当我点击跳转至一个带有EditText的界面后,模拟器中的软键盘会自动弹出,严重影响了用户体验。在网上找了资料,现总结如下。

    我们知道,EditText有一个 android:focusable=""的属性,但是如果你在edittext中直接将这个属性设置为true的话,点进去软键盘确实不会再弹出,但是EditText相应的也失去了聚焦,即无论你怎么点击它都不会有反应,这也就失去了EditText的原本的作用。那么要解决这个问题其实很简单,只需在EditText的父控件中加上这两行代码即可:

    android:focusable="true" 

    android:focusableInTouchMode="true"

    完整布局:

    <RelativeLayout
    android:background="@drawable/holiday_search"
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    android:layout_marginLeft="50dp"
    android:layout_alignParentRight="true"
    android:layout_marginRight="20dp"
    android:layout_centerVertical="true"
    android:focusable="true"
    android:focusableInTouchMode="true"
    >
    <ImageView
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="5dp"
    android:layout_centerVertical="true"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:gravity="center_vertical"
    android:background="@drawable/abs__ic_search"
    />
    <EditText
    android:singleLine="true"
    android:textSize="13sp"
    android:hint="输入景点名或城市名"
    android:background="#00ff00ff"
    android:gravity="center_vertical"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="30dp"
    android:layout_width="fill_parent"
    android:layout_height="30dp"
    />
    <ImageButton
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_alignParentRight="true"
    android:layout_marginRight="5dp"
    android:layout_centerVertical="true"
    android:background="@drawable/abs__ic_voice_search_api_holo_light"
    />
    </RelativeLayout>

    那么为什么这样加就没有问题了呢?我们知道父控件实际上并不需要获得聚焦,我们将父控件的focusable设置为false之后就使得整个父控件属于失去聚焦的状态,当我们点击跳转时软键盘也就不会再自动弹出,相应的,当我们需要对EditText进行编辑时也不会有影响。

  • 相关阅读:
    [Node.js]连接mongodb
    Vue中computed,methods,watch用法上的异同
    Vue中computed,methods,watch用法上的异同
    Vue method与computed的区别
    60分钟学会使用Node.js+Express+Ejs+mongoDB
    vue.js移动端app实战3:从一个购物车入门vuex
    基于Vue + Node.js + MongoDB的图片上传组件,实现图片的预览和删除
    【Java】线程的 6 种状态
    【Java】线程的创建方式
    如何愉快地通过命令安装Python库
  • 原文地址:https://www.cnblogs.com/dream-cichan/p/aaaa.html
Copyright © 2011-2022 走看看