如何使EditText在创buildActivity时不被重点关注
在您评分并标记为重复之前,请阅读我的问题。
我已经阅读了讨论这个问题的其他问题, 除了第一个创build的外 ,所有这些问题都适用于我的布局。
目前,这是我的onCreate方法的顶部: 
 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
  ^这样做至less键盘不会在启动时popup,但EditText仍然是重点。 
 这是我的EditText的XML : 
 <EditText android:id="@+id/password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/changePass" android:layout_centerHorizontal="true" android:layout_marginTop="167dp" android:ems="10" android:imeOptions="flagNoExtractUi" android:inputType="textPassword" android:maxLength="30" > </EditText> 
这就是我提出自己的活动时的样子:

 问题在于,对于某些电话,当EditText聚焦时,他们无法写入。 我希望它不重点。 
 我所做的工作,为下一个布局提出的,它的EditTexts不关注,看起来更像这样: 

 请注意,这是相同的布局,但。 这是用户返回到这个屏幕后的屏幕,这表明XML没什么问题,因为这是相同的XML ,但问题在于EditText只关注于何时创build活动。 
 我已经做了研究,所有其他的问题都不帮助我(但他们确实帮助键盘不显示,谢天谢地)。 我怎样才能使它启动EditText看起来像第二个截图,而不是第一个? 
 你可以设置布局的属性像android:descendantFocusability="beforeDescendants"和android:focusableInTouchMode="true" 
例:
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mainLayout" android:descendantFocusability="beforeDescendants" android:focusableInTouchMode="true" > <EditText android:id="@+id/password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/changePass" android:layout_centerHorizontal="true" android:layout_marginTop="167dp" android:ems="10" android:imeOptions="flagNoExtractUi" android:inputType="textPassword" android:maxLength="30" > </EditText> </RelativeLayout> 
愿这个人有用;)
XML代码:
 <EditText android:id="@+id/password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/changePass" android:layout_centerHorizontal="true" android:layout_marginTop="167dp" android:ems="10" android:focusable="false" android:imeOptions="flagNoExtractUi" android:inputType="textPassword" android:maxLength="30" > </EditText> 
Java代码:
 EditText edPwd = (EditText)findViewById(R.id.password); edtPwd.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { v.setFocusable(true); v.setFocusableInTouchMode(true); return false; } }); 
在xml中设置focusable false,并通过代码将其设置为true
在你的main_layout中添加这两行:
 android:descendantFocusability="beforeDescendants" android:focusableInTouchMode="true" 
例:
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:descendantFocusability="beforeDescendants" android:focusableInTouchMode="true"> *YOUR LAYOUT CODE* </RelativeLayout> 
XML代码
 <EditText android:imeOptions="flagNoExtractUi" android:focusable="false" /> 
onClickListerner中的Java代码
  mEdtEnterPhoneNumber.setFocusable(true); mEdtEnterPhoneNumber.setFocusableInTouchMode(true); 
添加onCreate()
 getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
或者在onCreateView()
  getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 
 你可以通过编程来完成。 在你的activity的onStart()方法中使用editText.setEnabled(false) (而不是在onCreate() – 因为这是初始化GUI组件的方法) 
 可以使用android:focusable="false"来禁用它,但是如果由于某种原因无法工作,那么您可以简单地放置一个LinearLayout ,它将在不中断布局的情况下占用焦点。 
 注意:Eclipse会给你一个错误,说你的LinearLayout是无用的,因为它没有内容。 你应该可以无视这个问题。 
例如:
 <LinearLayout android:focusable="true" android:focusableInTouchMode="false" android:layout_width="0dp" android:layout_height="0dp" /> <EditText android:id="@+id/password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/changePass" android:layout_centerHorizontal="true" android:layout_marginTop="167dp" android:ems="10" android:imeOptions="flagNoExtractUi" android:inputType="textPassword" android:maxLength="30" > </EditText> </LinearLayout> 
最好的解决scheme是在这里: https : //stackoverflow.com/a/45139132/3172843
正确和简单的解决scheme是setFocusable false和setFocusableInTouchMode为true 。 所以EditText增益只在用户触摸EditText时才会聚焦
 android:focusable="false" android:focusableInTouchMode="true" 
 <activity android:name=".MainActivity" android:windowSoftInputMode="stateAlwaysHidden" /> 
机器人:windowSoftInputMode = “stateAlwaysHidden”
将此行添加到Manifest.xml文件的activity标签中。当您单击TextEdit时,键盘将变为焦点。
 使用android:focusable="false"进行聚焦禁用 
 <EditText android:id="@+id/password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/changePass" android:layout_centerHorizontal="true" android:layout_marginTop="167dp" android:ems="10" android:imeOptions="flagNoExtractUi" android:inputType="textPassword" android:maxLength="30" android:focusable="false" > </EditText>