zoukankan      html  css  js  c++  java
  • 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下面的布局之中。

  • 相关阅读:
    Sql Server 2005 遍历结果集方法之一
    WinForm 程序托盘及右键退出
    WinForm 程序托盘
    JavaScript 简单定时器原理
    利用Httphandler实现URL重写(重写URL及伪静态)
    CYSCode 生成ORM框架的属性代码
    WinForm开机启动 判断 设置
    同级元素,鼠标经过高亮,鼠标离开还原(除选中已高亮元素时),点击确定高亮
    JavaScript 获取地址栏参数值
    openssl aes 加解密
  • 原文地址:https://www.cnblogs.com/cbx17v/p/6930790.html
Copyright © 2011-2022 走看看