EditText输入内容不显示

正常显示效果应该是这样:

在9.0系统显示异常,现象如下:

问题复现

可以看到,上面的两个输入框输入字符的时候都是实时显示的,当光标移动到最下方的输入框的时候,光标都停止跳动了,而且输入字符不会实时显示,当键盘收回的时候字符又出现了。

先上解决办法,有两种:

第一种:将 android:windowSoftInputMode 设置为 "adjustResize|stateHidden" (不推荐)

android:windowSoftInputMode="adjustResize|stateHidden" 
 
 
  1. <application

  2. ...

  3. android:hardwareAccelerated= "false"

  4. android:theme= "@style/Theme.AppCompat.Light.NoActionBar">

  5.  
  6. <activity

  7. android:name= ".activity.LoginActivity"

  8. ...

  9. android:windowSoftInputMode= "adjustUnspecified|stateHidden" />

  10. </application>

第二种:开启硬件加速

经过我多方测试,发现问题就是硬件加速没有开启导致的

(WTF,一般开了硬件加速才会有问题,9.0不开会有问题,WTF

1. 在application下开启硬件加速(不用说也知道不推荐,但是的确好使)

 
  1. <application

  2. ...

  3. android:hardwareAccelerated= "false"

  4. android:theme= "@style/Theme.AppCompat.Light.NoActionBar">

  5.  
  6. <activity

  7. android:name= ".activity.LoginActivity"

  8. ...

  9. android:windowSoftInputMode= "adjustUnspecified|stateHidden" />

  10. </application>

2. 在activity节点下开启硬件加速(可以,推荐)

 
  1. <application

  2. ...

  3. android:hardwareAccelerated= "false"

  4. android:theme= "@style/Theme.AppCompat.Light.NoActionBar">

  5.  
  6. <activity

  7. android:name= ".activity.LoginActivity"

  8. ...

  9. android:hardwareAccelerated= "true"

  10. android:windowSoftInputMode= "adjustUnspecified|stateHidden" />

  11. </application>

3. 对指定的view开启硬件加速(强烈推荐)

        EidtText.setLayerType(View.LAYER_TYPE_HARDWARE,null);
 

以上三种请根据需要任选其一

分析问题:

(刚开始没有找对方向,走了些弯路,下面写的比较啰嗦,不喜欢的的可以跳过)

原布局文件如下:

 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  2. android:layout_width= "match_parent"

  3. android:layout_height= "match_parent"

  4. android:layout_centerInParent= "true"

  5. android:layout_centerHorizontal= "true"

  6. android:layout_centerVertical= "true"

  7. android:paddingLeft= "40dp"

  8. android:paddingRight= "40dp"

  9. android:background= "@color/white_color"

  10. android:focusable= "true"

  11.  
  12. android:focusableInTouchMode= "true"

  13. android:gravity= "center"

  14. android:orientation= "vertical">

  15.  
  16. <ImageView

  17. android:layout_width= "96dp"

  18. android:layout_height= "94dp"

  19. android:background= "#99666666" />

  20.  
  21. <TextView

  22. android:layout_width= "wrap_content"

  23. android:layout_height= "wrap_content"

  24. android:layout_marginTop= "5dp"

  25. android:text= "@string/app_name"

  26. android:textColor= "#585858"

  27. android:textSize= "20sp"

  28. android:visibility= "invisible" />

  29.  
  30. <LinearLayout

  31. android:layout_width= "match_parent"

  32. android:layout_height= "wrap_content"

  33. android:layout_marginTop= "25dp"

  34. android:orientation= "vertical">

  35.  
  36. <TextView

  37. android:layout_width= "wrap_content"

  38. android:layout_height= "wrap_content"

  39. android:layout_marginLeft= "5dp"

  40. android:layout_marginBottom= "5dp"

  41. android:text= "企业号:"

  42. android:textSize= "14sp" />

  43.  
  44. <ImageView

  45. android:id= "@+id/iv_company"

  46. android:layout_width= "40dp"

  47. android:layout_height= "40dp"

  48. android:padding= "4dp"

  49. android:scaleType= "fitXY"

  50. android:src= "@drawable/ic_title_home_default"

  51. android:visibility= "gone" />

  52.  
  53. <EditText

  54. android:id= "@+id/et_company_login"

  55. android:layout_width= "match_parent"

  56. android:layout_height= "match_parent"

  57. android:layout_marginLeft= "5dp"

  58. android:background= "@drawable/selector_login_edittext"

  59. android:hint= "请输入企业号"

  60. android:imeOptions= "actionNext"

  61. android:lines= "1"

  62. android:padding= "10dp"

  63. android:paddingLeft= "5dp"

  64. android:singleLine= "true"

  65. android:textColor= "#585858"

  66. android:textSize= "14sp" />

  67. </LinearLayout>

  68.  
  69. <View

  70. android:id= "@+id/view_userfocus_login"

  71. android:layout_width= "match_parent"

  72. android:layout_height= "1dp"

  73. android:background= "@color/white_color" />

  74.  
  75. <LinearLayout

  76. android:layout_width= "match_parent"

  77. android:layout_height= "wrap_content"

  78. android:layout_marginTop= "10dp"

  79. android:orientation= "vertical">

  80.  
  81. <ImageView

  82. android:id= "@+id/iv_user_login"

  83. android:layout_width= "40dp"

  84. android:layout_height= "40dp"

  85. android:padding= "4dp"

  86. android:scaleType= "fitXY"

  87. android:src= "@drawable/user_white"

  88. android:visibility= "gone" />

  89.  
  90. <TextView

  91. android:layout_width= "wrap_content"

  92. android:layout_height= "wrap_content"

  93. android:layout_marginLeft= "5dp"

  94. android:layout_marginBottom= "5dp"

  95. android:text= "用户名:"

  96. android:textSize= "14sp" />

  97.  
  98. <EditText

  99. android:id= "@+id/et_user_login"

  100. android:layout_width= "match_parent"

  101. android:layout_height= "match_parent"

  102. android:layout_marginLeft= "5dp"

  103. android:background= "@drawable/selector_login_edittext"

  104. android:hint= "用户名"

  105. android:imeOptions= "actionNext"

  106. android:lines= "1"

  107. android:padding= "10dp"

  108. android:paddingLeft= "5dp"

  109. android:singleLine= "true"

  110. android:textColor= "#585858"

  111. android:textSize= "14sp" />

  112. </LinearLayout>

  113.  
  114. <View

  115. android:layout_width= "match_parent"

  116. android:layout_height= "1dp"

  117. android:background= "@color/white_color" />

  118.  
  119. <LinearLayout

  120. android:layout_width= "match_parent"

  121. android:layout_height= "wrap_content"

  122. android:layout_marginTop= "10dp"

  123. android:orientation= "vertical">

  124.  
  125. <ImageView

  126. android:id= "@+id/iv_password_login"

  127. android:layout_width= "40dp"

  128. android:layout_height= "40dp"

  129. android:padding= "4dp"

  130. android:scaleType= "fitXY"

  131. android:src= "@drawable/password_white"

  132. android:visibility= "gone" />

  133.  
  134. <TextView

  135. android:layout_width= "wrap_content"

  136. android:layout_height= "wrap_content"

  137. android:layout_marginLeft= "5dp"

  138. android:layout_marginBottom= "5dp"

  139. android:text= "密码:"

  140. android:textSize= "14sp" />

  141.  
  142. <EditText

  143. android:id= "@+id/et_password_login"

  144. android:layout_width= "match_parent"

  145. android:layout_height= "40dp"

  146. android:layout_marginLeft= "5dp"

  147. android:background= "@drawable/selector_login_edittext"

  148. android:hint= "密码"

  149. android:imeOptions= "actionDone"

  150. android:inputType= "textPassword"

  151. android:lines= "1"

  152. android:padding= "10dp"

  153. android:paddingLeft= "5dp"

  154. android:singleLine= "true"

  155. android:textColor= "#585858"

  156. android:textSize= "14sp" />

  157. </LinearLayout>

  158.  
  159. <View

  160. android:id= "@+id/view_pwdfocus_login"

  161. android:layout_width= "match_parent"

  162. android:layout_height= "1dp"

  163. android:background= "@color/white_color" />

  164.  
  165. <TextView

  166. android:id= "@+id/btn_login"

  167. android:layout_width= "match_parent"

  168. android:layout_height= "wrap_content"

  169. android:layout_marginTop= "30dp"

  170. android:background= "@drawable/selector_greem_btn_bg"

  171. android:gravity= "center"

  172. android:padding= "8dp"

  173. android:text= "登 录"

  174. android:textColor= "@color/white_color"

  175. android:textSize= "16sp" />

  176.  
  177. <RelativeLayout

  178. android:layout_width= "match_parent"

  179. android:layout_height= "40dp"

  180. android:layout_marginTop= "15dp">

  181.  
  182. <CheckBox

  183. android:id= "@+id/cb_remember_login"

  184. android:layout_width= "wrap_content"

  185. android:layout_height= "wrap_content"

  186. android:layout_alignParentStart= "true"

  187. android:layout_alignParentLeft= "true"

  188. android:layout_centerVertical= "true"

  189. android:background= "@drawable/selector_remember_login"

  190. android:button= "@null" />

  191.  
  192. <TextView

  193. android:layout_width= "wrap_content"

  194. android:layout_height= "wrap_content"

  195. android:layout_centerVertical= "true"

  196. android:layout_marginLeft= "5dp"

  197. android:layout_toRightOf= "@+id/cb_remember_login"

  198. android:text= "记住密码"

  199. android:textSize= "@dimen/text_moderate_size" />

  200.  
  201. <Button

  202. android:id= "@+id/btn_config_login"

  203. android:layout_width= "80dp"

  204. android:layout_height= "match_parent"

  205. android:layout_alignParentRight= "true"

  206. android:background= "@drawable/login_item_btn"

  207. android:onClick= "onConfig"

  208. android:text= "系统配置"

  209. android:textColor= "#149048"

  210. android:textSize= "@dimen/text_moderate_size"

  211. android:visibility= "gone" />

  212. </RelativeLayout>

  213. </LinearLayout>

原清单文件:

 
  1. <activity

  2. android:name= ".activity.LoginActivity"

  3. android:configChanges= "orientation|keyboardHidden|navigation|screenSize"

  4. android:icon= "@drawable/user_btn"

  5. android:launchMode= "singleTask"

  6. android:screenOrientation= "portrait"

  7. android:windowSoftInputMode= "adjustPan|stateHidden" />

同样的代码拷贝到demo里运行没有问题,项目里运行就有问题
 

随后将清单文件简化如下:

 
  1. <activity

  2. android:name= ".activity.LoginActivity"

  3. android:windowSoftInputMode= "adjustPan|stateHidden" />

        <activity android:name=".activity.LoginActivity"/>
 
 
  1. <activity

  2. android:name= ".activity.LoginActivity"

  3. android:windowSoftInputMode= "adjustUnspecified|stateHidden" />

问题都没有解决,随后猜测:

一、是不是java文件的某些代码造成的?

二、是不是键盘弹出对输入框有遮挡?

验证第一个猜想:

1.将布局中的第二个输入框用户名和第三个输入框密码调换位置,运行后发现:密码输入框正常,用户名输入框产生了同样的问题;

2.将布局中的密码输入框复制一份在最后,去掉控件的 id,运行后发现:两个密码框都出现了上述问题;

3.将LoginActivity改成如下:

 
  1. public class LoginActivity extends Activity {

  2. @Override

  3. protected void onCreate(@Nullable Bundle savedInstanceState) {

  4. super.onCreate(savedInstanceState);

  5. setContentView(R.layout.activity_login);

  6. }

  7. }

运行后发现:问题依然存在

结论:该问题与java代码无关

验证第二个猜想:

将多个EditText竖直方向并列放在LinearLayout中,外层包裹一个NestedScrollView,运行后出现了神奇的现象:

输入的时候没有显示字符,上划的时候输入框里字符出现了,而且留下了半拉框在键盘上方,

猜你喜欢

转载自blog.csdn.net/guodashen007/article/details/108768508
今日推荐