Android进入一个新页面,EditText失去焦点并禁止弹出键盘

android在进入一个新页面后,edittext会自动获取焦点并弹出软键盘,这样并不符合用户操作习惯。

在其父控件下,添加如下的属性,就可以完美解决,使其进入页面后不主动获取焦点,并且不弹出软键盘:

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

代码如下:

  1 <ScrollView
  2     android:layout_width="match_parent"
  3     android:layout_height="match_parent">
  4
  5     <LinearLayout
  6         android:layout_width="match_parent"
  7         android:layout_height="match_parent"
  8         android:focusable="true"
  9         android:focusableInTouchMode="true"
 10         android:orientation="vertical">
 11         <!--表单-->
 12         <LinearLayout
 13             android:layout_width="match_parent"
 14             android:layout_height="wrap_content"
 15             android:background="@color/white"
 16             android:orientation="vertical">
 17
 18             <View
 19                 android:layout_width="match_parent"
 20                 android:layout_height="14dp"
 21                 android:background="@color/stroke_color" />
 22
 23             <View
 24                 android:layout_width="match_parent"
 25                 android:layout_height="1dp"
 26                 android:background="@color/stroke_color" />
 27
 28             <TextView
 29                 android:id="@+id/tv_title_message"
 30                 android:layout_width="wrap_content"
 31                 android:layout_height="wrap_content"
 32                 android:layout_marginBottom="18dp"
 33                 android:layout_marginLeft="21dp"
 34                 android:layout_marginTop="18dp"
 35                 android:text="@string/title_coming_menu"
 36                 android:textColor="@color/font_dark_gray"
 37                 android:textSize="18sp" />
 38
 39             <View
 40                 android:layout_width="match_parent"
 41                 android:layout_height="1dp"
 42                 android:background="@color/stroke_color" />
 43
 44             <LinearLayout
 45                 android:layout_width="match_parent"
 46                 android:layout_height="match_parent"
 47                 android:orientation="vertical">
 48
 49                 <LinearLayout
 50                     android:layout_width="match_parent"
 51                     android:layout_height="wrap_content"
 52                     android:orientation="horizontal">
 53
 54                     <RelativeLayout
 55                         android:layout_width="0dp"
 56                         android:layout_height="wrap_content"
 57                         android:layout_weight="1">
 58
 59                         <TextView
 60                             android:id="@+id/FromMessage"
 61                             android:layout_width="wrap_content"
 62                             android:layout_height="wrap_content"
 63                             android:layout_marginBottom="16dp"
 64                             android:layout_marginLeft="21dp"
 65                             android:layout_marginTop="21dp"
 66                             android:text="@string/title_coming_unit"
 67                             android:textColor="@color/font_gray"
 68                             android:textSize="16sp" />
 69
 70                         <TextView
 71                             android:id="@+id/txtFromMessage"
 72                             android:layout_width="wrap_content"
 73                             android:layout_height="wrap_content"
 74                             android:layout_alignBottom="@+id/FromMessage"
 75                             android:layout_toRightOf="@+id/FromMessage"
 76                             android:textColor="@color/activity_dark3"
 77                             android:textSize="16sp" />
 78                     </RelativeLayout>
 79
 80                     <TextView
 81                         android:layout_width="1px"
 82                         android:layout_height="match_parent"
 83                         android:layout_marginTop="13dp"
 84                         android:background="@color/plan_detail_line" />
 85
 86                     <RelativeLayout
 87                         android:layout_width="0dp"
 88                         android:layout_height="wrap_content"
 89                         android:layout_weight="1">
 90
 91                         <TextView
 92                             android:id="@+id/FromMessageNo"
 93                             android:layout_width="wrap_content"
 94                             android:layout_height="wrap_content"
 95                             android:layout_marginBottom="16dp"
 96                             android:layout_marginLeft="17dp"
 97                             android:layout_marginTop="21dp"
 98                             android:text="@string/title_coming_number"
 99                             android:textColor="@color/font_gray"
100                             android:textSize="16sp" />
101
102                         <TextView
103                             android:id="@+id/txtFromMessageNo"
104                             android:layout_width="wrap_content"
105                             android:layout_height="wrap_content"
106                             android:layout_alignBottom="@+id/FromMessageNo"
107                             android:layout_toRightOf="@id/FromMessageNo"
108                             android:textColor="@color/activity_dark3"
109                             android:textSize="16sp" />
110                     </RelativeLayout>
111                 </LinearLayout>
112
113                 <LinearLayout
114                     android:layout_width="match_parent"
115                     android:layout_height="wrap_content"
116                     android:orientation="horizontal">
117
118                     <RelativeLayout
119                         android:layout_width="0dp"
120                         android:layout_height="wrap_content"
121                         android:layout_marginTop="8dp"
122                         android:layout_weight="1">
123
124                         <TextView
125                             android:id="@+id/Signer"
126                             android:layout_width="wrap_content"
127                             android:layout_height="wrap_content"
128                             android:layout_marginBottom="16dp"
129                             android:layout_marginLeft="21dp"
130                             android:text="@string/title_coming_signer"
131                             android:textColor="@color/font_gray"
132                             android:textSize="16sp" />
133
134                         <TextView
135                             android:id="@+id/txtSigner"
136                             android:layout_width="wrap_content"
137                             android:layout_height="wrap_content"
138                             android:layout_alignBottom="@+id/Signer"
139                             android:layout_toRightOf="@+id/Signer"
140                             android:textColor="@color/activity_dark3"
141                             android:textSize="16sp" />
142                     </RelativeLayout>
143
144                     <TextView
145                         android:layout_width="1px"
146                         android:layout_height="match_parent"
147                         android:background="@color/plan_detail_line" />
148
149                     <RelativeLayout
150                         android:layout_width="0dp"
151                         android:layout_height="wrap_content"
152                         android:layout_marginTop="8dp"
153                         android:layout_weight="1">
154
155                         <TextView
156                             android:id="@+id/FilePriorities"
157                             android:layout_width="wrap_content"
158                             android:layout_height="wrap_content"
159                             android:layout_marginBottom="16dp"
160                             android:layout_marginLeft="17dp"
161                             android:text="@string/title_coming_priority"
162                             android:textColor="@color/font_gray"
163                             android:textSize="16sp" />
164
165                         <TextView
166                             android:id="@+id/txtFilePriorities"
167                             android:layout_width="wrap_content"
168                             android:layout_height="wrap_content"
169                             android:layout_alignBottom="@+id/FilePriorities"
170                             android:layout_toRightOf="@+id/FilePriorities"
171                             android:textColor="@color/activity_dark3"
172                             android:textSize="16sp" />
173                     </RelativeLayout>
174                 </LinearLayout>
175
176                 <LinearLayout
177                     android:layout_width="match_parent"
178                     android:layout_height="wrap_content"
179                     android:orientation="horizontal">
180
181                     <RelativeLayout
182                         android:layout_width="0dp"
183                         android:layout_height="wrap_content"
184                         android:layout_weight="1">
185
186                         <TextView
187                             android:id="@+id/ReceiptNumber"
188                             android:layout_width="wrap_content"
189                             android:layout_height="wrap_content"
190                             android:layout_marginBottom="21dp"
191                             android:layout_marginLeft="21dp"
192                             android:text="@string/title_coming_identifier"
193                             android:textColor="@color/font_gray"
194                             android:textSize="16sp" />
195
196                         <TextView
197                             android:id="@+id/txtReceiptNumber"
198                             android:layout_width="wrap_content"
199                             android:layout_height="wrap_content"
200                             android:layout_alignBottom="@+id/ReceiptNumber"
201                             android:layout_toRightOf="@+id/ReceiptNumber"
202                             android:textColor="@color/activity_dark3"
203                             android:textSize="16sp" />
204                     </RelativeLayout>
205
206                     <TextView
207                         android:layout_width="1px"
208                         android:layout_height="match_parent"
209                         android:layout_marginBottom="13dp"
210                         android:background="@color/plan_detail_line" />
211
212                     <RelativeLayout
213                         android:layout_width="0dp"
214                         android:layout_height="wrap_content"
215                         android:layout_weight="1" />
216                 </LinearLayout>
217             </LinearLayout>
218
219             <View
220                 android:layout_width="match_parent"
221                 android:layout_height="1dp"
222                 android:background="@color/stroke_color" />
223
224             <View
225                 android:layout_width="match_parent"
226                 android:layout_height="14dp"
227                 android:background="@color/stroke_color" />
228
229             <View
230                 android:layout_width="match_parent"
231                 android:layout_height="1dp"
232                 android:background="@color/stroke_color" />
233         </LinearLayout>
234
235         <LinearLayout
236             android:layout_width="match_parent"
237             android:layout_height="wrap_content"
238             android:background="@color/white"
239             android:orientation="vertical">
240
241             <View
242                 android:layout_width="match_parent"
243                 android:layout_height="1dp"
244                 android:background="@color/stroke_color" />
245
246             <TextView
247                 android:layout_width="wrap_content"
248                 android:layout_height="wrap_content"
249                 android:layout_marginBottom="18dp"
250                 android:layout_marginLeft="21dp"
251                 android:layout_marginTop="18dp"
252                 android:text="@string/title_signfor_branch"
253                 android:textColor="@color/font_dark_gray"
254                 android:textSize="18sp" />
255
256             <View
257                 android:layout_width="match_parent"
258                 android:layout_height="1dp"
259                 android:background="@color/stroke_color" />
260
261             <RelativeLayout
262                 android:id="@+id/rlFileType"
263                 android:layout_width="match_parent"
264                 android:layout_height="wrap_content">
265
266                 <TextView
267                     android:id="@+id/txtFileType"
268                     android:layout_width="wrap_content"
269                     android:layout_height="wrap_content"
270                     android:layout_marginBottom="18dp"
271                     android:layout_marginLeft="21dp"
272                     android:layout_marginTop="24dp"
273                     android:text="@string/title_file_type"
274                     android:textColor="@color/font_gray"
275                     android:textSize="16sp" />
276
277                 <RadioGroup
278                     android:id="@+id/rgFileType"
279                     android:layout_width="match_parent"
280                     android:layout_height="wrap_content"
281                     android:layout_marginTop="19dp"
282                     android:layout_toRightOf="@+id/txtFileType"
283                     android:checkedButton="@+id/rbDoArticle"
284                     android:orientation="horizontal">
285
286                     <RadioButton
287                         android:id="@+id/rbDoArticle"
288                         style="@style/FileTypeRadioButtonNew"
289                         android:text="@string/title_fileType_branch" />
290
291                     <RadioButton
292                         android:id="@+id/rbReadPiece"
293                         style="@style/FileTypeRadioButtonNew"
294                         android:layout_marginLeft="20dp"
295                         android:text="@string/title_fileType_read_piece" />
296
297                     <RadioButton
298                         android:id="@+id/rbTelegram"
299                         style="@style/FileTypeRadioButtonNew"
300                         android:layout_marginLeft="20dp"
301                         android:text="@string/title_fileType_telegram" />
302
303                     <RadioButton
304                         android:id="@+id/rbElse"
305                         style="@style/FileTypeRadioButtonNew"
306                         android:layout_marginLeft="20dp"
307                         android:text="@string/title_fileType_another" />
308                 </RadioGroup>
309             </RelativeLayout>
310
311             <RelativeLayout
312                 android:id="@+id/rlPoints"
313                 android:layout_width="match_parent"
314                 android:layout_height="wrap_content">
315
316                 <TextView
317                     android:id="@+id/Points"
318                     android:layout_width="wrap_content"
319                     android:layout_height="wrap_content"
320                     android:layout_marginLeft="21dp"
321                     android:layout_marginTop="18dp"
322                     android:text="@string/title_branch_department"
323                     android:textColor="@color/font_gray"
324                     android:textSize="16sp" />
325
326                 <TextView
327                     android:id="@+id/txtPoints"
328                     android:layout_width="wrap_content"
329                     android:layout_height="wrap_content"
330                     android:layout_marginTop="16dp"
331                     android:layout_toRightOf="@+id/Points"
332                     android:background="@drawable/shape_tv_signfor_pointfor"
333                     android:paddingBottom="3dp"
334                     android:paddingLeft="8dp"
335                     android:paddingRight="8dp"
336                     android:paddingTop="3dp"
337                     android:text="@string/content_pointfor"
338                     android:textColor="@color/flowmsg_doing" />
339             </RelativeLayout>
340
341             <RelativeLayout
342                 android:id="@+id/rlOpinion"
343                 android:layout_width="match_parent"
344                 android:layout_height="wrap_content">
345
346                 <TextView
347                     android:id="@+id/Opinion"
348                     android:layout_width="wrap_content"
349                     android:layout_height="wrap_content"
350                     android:layout_marginLeft="21dp"
351                     android:layout_marginTop="18dp"
352                     android:text="@string/title_branch_advice"
353                     android:textColor="@color/font_gray"
354                     android:textSize="16sp" />
355
356                 <EditText
357                     android:id="@+id/txtOpinion"
358                     android:layout_width="match_parent"
359                     android:layout_height="200dp"
360                     android:layout_marginRight="20dp"
361                     android:layout_marginTop="19dp"
362                     android:layout_toRightOf="@+id/Opinion"
363                     android:background="@drawable/shape_et_bg"
364                     android:gravity="top"
365                     android:hint="@string/hint_write_branch_advice"
366                     android:padding="9dp"
367                     android:textColor="@color/tab_text"
368                     android:textSize="14sp" />
369             </RelativeLayout>
370
371
372             <LinearLayout
373                 android:layout_width="match_parent"
374                 android:layout_height="wrap_content"
375                 android:layout_marginBottom="40dp"
376                 android:layout_marginTop="40dp"
377                 android:gravity="center">
378
379                 <Button
380                     android:id="@+id/tv_sign_for"
381                     android:layout_width="250dp"
382                     android:layout_height="50dp"
383                     android:background="@drawable/selector_btn"
384                     android:text="@string/btn_signfor_branch"
385                     android:textAppearance="@style/common_text_size"
386                     android:textColor="@color/white" />
387
388                 <Button
389                     android:id="@+id/tv_retreat"
390                     android:layout_width="250dp"
391                     android:layout_height="50dp"
392                     android:layout_marginLeft="40dp"
393                     android:background="@drawable/selector_btn_3"
394                     android:text="@string/btn_retreat"
395                     android:textAppearance="@style/common_text_size"
396                     android:textColor="@color/flowmsg_doing" />
397             </LinearLayout>
398         </LinearLayout>
399     </LinearLayout>
400 </ScrollView>

不一定只加在父级布局中,如果没有效果,可以试试更高级的父级布局中,就像示例代码中展示的一样,写在scrollview下面的布局之中。

时间: 2024-08-02 15:14:36

Android进入一个新页面,EditText失去焦点并禁止弹出键盘的相关文章

IPhone手机页面中点击文本输入框,弹出键盘,网页会放大,如何解决

在head标签中加入以上meta声明.具体属性可以谷歌/百度. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> 我查了下viewport,有几个属性:width - viewport的宽度 height - viewport的高度initial-scale - 初始的缩放比例minim

Android中使EditText失去焦点,edittext禁止弹出键盘

在我们的应用中,有时候一进入一个页面, EditText默认就会自动获取焦点.弹出输入法框,用户体验很不好, 那么如何取消这个默认行为呢? ps:这篇文字是一年前写的,现在有网友再问这个问题,我进行重新编辑--2014.05.07,目前有更好的办法,第一种方法局限性很强,大家可以使用第二种方法 第一种方法:.在网上找了好久,有点监听软键盘事件的方法,有调用 clearFouse()方法,但是测试了都不行!在对应的 xml中也找不到相应的属性可以关闭这个默认行为. 后来研究了一下,在其父控件下,添

Android中使EditText失去焦点,edittext禁止弹出键盘[转]

转自http://www.cnblogs.com/yejiurui/archive/2013/01/02/2841945.html 在我们的应用中,有时候一进入一个页面, EditText默认就会自动获取焦点.弹出输入法框,用户体验很不好, 那么如何取消这个默认行为呢? ps:这篇文字是一年前写的,现在有网友再问这个问题,我进行重新编辑--2014.05.07,目前有更好的办法,第一种方法局限性很强,大家可以使用第二种方法 第一种方法:.在网上找了好久,有点监听软键盘事件的方法,有调用 clea

H5页面 input禁止弹出键盘

$('#t').on("focus",function(){ document.activeElement.blur();//屏蔽默认键盘弹出: });

JS解析Json 数据并跳转到一个新页面,取消A 标签跳转

JS解析Json 数据并跳转到一个新页面,代码如下 $.getJSON("http://api.cn.abb.com/common/api/staff/employee/" + obj.id, function (result) { window.open("https://abb-my.sharepoint.com/_layouts/15/me.aspx?p=" + result.Email, "_blank") }); 取消A 标签跳转 &l

关于android中EditText自动获取焦点并弹出键盘的相关设置

在android开发中,关于EditText自动获取焦点弹出键盘,我们可能又是会有让键盘自动弹出的需求,有时可能又会有不想让键盘自动弹出的需求,下面是我所总结的两种方法: 需求:EditText自动获取焦点并弹出键盘,代码: EditText.setFocusable(true); EditText.setFocusableInTouchMode(true); EditText.requestFocus(); 需求:EditText不会自动获取焦点并且不会弹出键盘,代码:  将其父控件设置: P

Android弹出键盘布局闪动原理和解决

弹出键盘布局闪动原理和解决 在开发中,遇到一个问题:做一个微信一样,表情输入和软键盘在切换的时候,聊天界面不闪动的问题.为了解决这个问题,需要知道一下Android的软键盘弹出的时候发生的几个变化. 当AndroidMainfest.xml 中配置android:windowSoftInputMode="adjustResize|stateHidden" 属性后,如果弹出软键盘,那么会重绘界面.基本流程如下(API 10): 1.  Android 收到打开软键盘命令 2.  Andr

android创建一个细节页面,以及argument的应用

大家好..以前我也好奇过有没有一些设计模式可以用来创建一个细节页面,直到最近我看了一些设计模式相关的博文和书后,我终于发现了以下的应用. 下面我们介绍一个Listview 点击后出现一个detail 页面的功能,功能简单,但是我们看的是思想. 首先我们实现自定义的ListView,ListItem 里面有一个TextView,一个EditText,一个Checkbox,对应存放数据的是一个Crime的类.ListView里面有好多个ListItem,而对应的是CrimeList 类,为了方便创建

Android 让EditText失去焦点避免自动弹出输入法

如果一进去activity,EditText就获取焦点,弹出输入法界面,无疑是很影响美观的.关于让EditText失去焦点,网上比较多的做法是添加一个visibility=gone的Textview.然后让这个textView获取焦点.不知道是我人品不好还是怎么的.我这样做不行,后来采用另外一种做法,就是在其父组件(布局)上添加以下两句代码: android:focusable="true" android:focusableInTouchMode="true" 比