现在的位置: 首页 > 综合 > 正文

EditText获得焦点时阻止输入法弹出

2014年02月21日 ⁄ 综合 ⁄ 共 1592字 ⁄ 字号 评论关闭

 

//EditText有焦点阻止输入法弹出  
            editText.setOnTouchListener(new OnTouchListener() {                    
                public boolean onTouch(View v, MotionEvent event) {  
                    // TODO Auto-generated method stub  
                    int inType = editText.getInputType(); // backup the input type  
                    editText.setInputType(InputType.TYPE_NULL); // disable soft input      
                    editText.onTouchEvent(event); // call native handler      
                    editText.setInputType(inType); // restore input type     
                    return true;                      
                }  
            }); 

在Android系统中,由于手机屏幕大小的限制,一般需要字符输入的时候,弹出的输入法面板往往会占据大半个屏幕,如果输入框正好在下方,那经常会出现被输入法面板遮挡的尴尬,给使用者带来不小的困扰,用户体验很不友好。
  查了一下Android SDK的说明,发现可以通过设置Activity的一个属性来解决这个问题,比如可以在AndroidManifest.xml中这样写:

  < activity android:name=”.CategoryList”
  android:label=”@string/app_name”
  android:windowSoftInputMode=”stateVisible|adjustPan” >
  < /activity >

这里面的android:windowSoftInputMode就是用来避免输入法面板遮挡问题的,具体的参数说明可参考SDK文档,但是,文档中说明,该属性是Android 1.5之后才加上的,也就是API Level 3以后的SDK才支持,那么如果在较早版本的android机器上就不管用了,则该怎么办呢?好在我又找到了另外一种解决办法,就是ScrollView。我们可以在对应的layout XML的顶级元素上加一层ScrollView,这样,在这ScrollView中,所有的文本框在输入法面板弹出的时候都会自动的向上滚动,示例代码如下:

  < ScrollView xmlns:android=”http://schemas.android.com/apk/res/android “
  android:layout_width=”fill_parent”
  android:layout_height=”fill_parent” >
  < LinearLayout android:orientation=”vertical”
  android:layout_width=”fill_parent”
  android:layout_height=”fill_parent” 
  < EditText android:id=”@+id/content”
  android:layout_width=”fill_parent”
  android:layout_height=”wrap_content”
  android:lines=”4″
  android:scrollbars=”vertical”
  android:gravity=”fill_horizontal” / >
  < /LinearLayout >
  < /ScrollView >

默认情况下,输入法弹出的时候,原来的view会被挤扁。有些应用不想被挤,它们可以接受被输入法view覆盖在上面。这时候需要在AndroidManifest.xml中的acitivty里面加上一句:android:windowSoftInputMode="adjustPan"

 

 

抱歉!评论已关闭.