Android取消EditText自动获取焦点

在Android应用中,遇到EditText默认获取焦点导致的困扰。本文介绍如何避免这种默认行为,探讨监听软键盘事件、clearFocus()方法以及XML属性设置等解决方案,最终提供一种有效的方法来禁止EditText自动获取焦点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android取消EditText自动获取焦点默认行为

效果图



最近在通讯录新建联系人=中,一进入一个页面, EditText默认就会自动获取焦点,很是郁闷, 如何让EditText不自动获取焦点?

那么如何取消这个默认行为呢?

在网上找了好久,有点 监听软键盘事件,有点 调用 clearFouse()方法,但是测试了都没有! xml中也找不到相应的属性可以关闭这个默认行为


解决之道:在EditText的父级控件中找一个,设置成

android:focusable="true" 
android:focusableInTouchMode="true"

demo.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:background="#ffffff"
 android:baselineAligned="false"
 android:focusable="true"
 android:focusableInTouchMode="true" > 
   <RelativeLayout
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:id="@+id/txtLayout"
    android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
    > 
   <EditText
    android:layout_width="fill_parent"
    android:layout_height="35dp"
    android:id="@+id/editPhone"
    android:phoneNumber="true"
    android:text="@string/txtaddPhone"
    android:textColor="#D8D8D8"
android:background="@drawable/txtupbg"
    android:textSize="15dp"
    /> 
   </RelativeLayout> 
</RelativeLayout>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值