Android 关于 EditText 的一些问题 如:不弹出输入法

方法一(此种有效,其余未测): 在 AndroidMainfest.xml 中 选 择 哪 个 activity , 设 置 windowSoftInputMode 属 性 为 adjustUnspecified|stateHidden 例如:

<activity             android:name=".Main"android:label="@string/appandroid:windowSoftInputMode="adjustUnspecified|stateHidden " android:configChanges="orientation|keyboardHidden"> 

<intent-filter> 

<action android:name="android.intent.action.MAIN" /> 

<category android:name="android.intent.category.LAUNCHER" /> 

</intent-filter> 

</activity>

 方法二: 让 EditText 失去焦点,使用 EditText 的 clearFocus 方法 例如:EditText edit=(EditText)findViewById(R.id.edit); edit.clearFocus(); 

方法三: 强制隐藏 Android 输入法窗口 例如:

EditText edit=(EditText)findViewById(R.id.edit); InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(edit.getWindowToken(),0); 

2.EditText 始终不弹出软件键盘 例:EditText edit=(EditText)findViewById(R.id.edit); edit.setInputType(InputType.TYPE_NULL);

Android 的 EditText inputtype
Android 中 inputType 属性在 EditText 输入值时启动的虚拟键盘的风格有着重要的作用。 这也大大的方便的操作。有时需要虚拟键盘只为字符或只为数字。所以 inputType 尤为重 要。 设置文本的类型,用于帮助输入法显示合适的键盘类型。 有如下值设置: none、text、 textCapCharacters 字母大小、 textCapWords 单词首字母大小、

textCapSentences 仅第一个字母大小、 textAutoCorrect、 textAutoComplete 自动完成、 textMultiLine 多行输入、 textImeMultiLine 输入法多行(如果支持)、 textNoSuggestions 不提示、 textEmailAddress 电子邮件地址、 textEmailSubject 邮件主题、 textShortMessage 短信息(会多一个表情按钮出来,点开如下图)、

textLongMessage 长讯息?、 textPersonName 人名、 textPostalAddress 地址、 textPassword 密码、 textVisiblePassword 可见密码(实现禁止输入中文)、 textWebEditText 作为网页表单的文本、 textFilte 文本筛选过滤、 textPhonetic 拼音输入、 numberSigned 有符号数字格式、 numberDecimal 可带小数点的浮点格式、 phone 电话号码、

datetime 时间日期、 date 日期、 time 时间 <EditText android:layout_width="fill_parent" android:layout_height="wrap_content"android:inputType="phone" /> //文本类型,多为大写、小写和数字符号。 android:inputType="none" android:inputType="text" android:inputType="textCapCharacters" android:inputType="textCapWords" android:inputType="textCapSentences" android:inputType="textAutoCorrect" android:inputType="textAutoComplete" android:inputType="textMultiLine" android:inputType="textImeMultiLine" android:inputType="textNoSuggestions" android:inputType="textUri" android:inputType="textEmailAddress" android:inputType="textEmailSubject" android:inputType="textShortMessage" android:inputType="textLongMessage" android:inputType="textPersonName" android:inputType="textPostalAddress" android:inputType="textPassword" android:inputType="textVisiblePassword" android:inputType="textWebEditText" android:inputType="textFilter" android:inputType="textPhonetic" //数值类型 android:inputType="number" android:inputType="numberSigned" android:inputType="numberDecimal" android:inputType="phone"//拨号键盘

android:inputType="datetime" android:inputType="date"//日期键盘 android:inputType="time"//时间键盘

android 软键盘隐藏总结
分类: android2012-04-10 10:26 172 人阅读 评论(0) 收藏 举报 androidservice 输入法 actionnull

1>软键盘的状态——隐藏或显示。 一:自动弹出软键盘

1. Timer timer=new Timer(); 2. 3. 4. 5. public void run() { InputMethodManager inputMethodManager=(InputMethodManager) get SystemService(Context.INPUT_METHOD_SERVICE); 6. inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_N OT_ALWAYS); 7. 8. } }, 2000); timer.schedule(new TimerTask() {

二:软键盘 Activity 中设置:

Android:windowSoftInputMode="stateUnspecified",默认设置: 软键盘的状态 (隐藏或可见)没有被指定。系统将选择一个合适的状态或依赖于主题的设 置。 藏。 "stateUnchanged", 软键盘被 "stateAlwaysHidden", 软键 保持上次的状态。 "stateHidden", 当用户选择该 Activity 时,软键盘被隐

盘总是被隐藏的。 "stateVisible",. 软键盘是可见 的。 "stateAlwaysVisible", 当用 户选择这个 Activity 时,软键盘是可见的。 "adjustUnspecified", . 它不被 指定是否该 Activity 主窗口调整大小以便留出软键盘的空间, 或是否窗口上的 内容得到屏幕上当前的焦点是可见的。 系统将自动选择这些模式中一种主要依赖 于是否窗口的内容有任何布局视图能够滚动他们的内容。 如果有这样的一个视 图, 这个窗口将调整大小, 这样的假设可以使滚动窗口的内容在一个较小的区域 中可见的。这个是主窗口默认的行为设置。也就是说, 系统自动决定是采用平 移模式还是压缩模式,决定因素在于内容是否可以滚 动。 "adjustResize", (压缩模式) 当软键盘弹出时,要对主窗口调整屏幕的大小以便留出软键盘的空间。 "adjustPan"] > (平移模式:当输入框不会被遮挡时,该模式没有对布局进行 调整,然而当输入框将要被遮挡时, 窗口就会进行平移。也就是说,该模式始 终是保持输入框为可见
三:隐藏软键盘: EditText edit=(EditText)findViewById(R.id.edit); InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(edit.getWindowToken(),0);

. . .

4.EditText 默认不弹出软件键盘 方法一: 在 AndroidMainfest.xml 中选择哪个 activity,设置 windowSoftInputMode 属性为 adjustUnspecified|stateHidden 例如:<activity Android:name=".Main" Android:label="@string/app_name" Android:windowSoftInputMode="adjustUnspecified|stateHidden" Android:configChanges="orientation|keyboardHidden"> <intent-filter> <action Android:name="android.intent.action.MAIN" /> <category Android:name="android.intent.category.LAUNCHER" />

</intent-filter> </activity> 方法二: 让 EditText 失去焦点,使用 EditText 的 clearFocus 方法 例如:EditText edit=(EditText)findViewById(R.id.edit); edit.clearFocus(); 方法三: 强制隐藏 Android 输入法窗口 例如:EditText edit=(EditText)findViewById(R.id.edit); InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(edit.getWindowToken(),0); 5.EditText 始终不弹出软件键盘 例:EditText edit=(EditText)findViewById(R.id.edit); edit.setInputType(InputType.TYPE_NULL);

Android 中 EditTex 焦点设置和弹 不弹出输入法的问题
今天编程碰到了一个问题:有一款平板,打开一个有 EditText 的 Activity 会默认弹出输入 法。 为了解决这个问题就深入研究了下 android 中焦点 Focus 和弹出输入法的问题。 在网上看了 些例子都不够全面,在这里全面总结下。 一:EditText 为什么会默认弹出输入法? 同样的代码,碰到有 EditText 控件的界面时有的机子会弹出输入法,有的机子不会弹 出。不好意思,这问题我也一头雾水,谁知道可以告诉我,否则我就把这个问题留下来,以后研 究 android 源码时再搞个清楚。但是...我有解决方案。 二:默认弹出和默认关闭输入法的解决方案。 1.默认关闭,不至于进入 Activity 就打开输入法,影响界面美观。 ①在布局文件中,在 EditText 前面放置一个看不到的 LinearLayout,让他率先获取焦点:

<LinearLayout android:focusable="true" android:focusableInTouchMode="true" android:layout_width="0px" android:layout_height="0px"/>

②方法二:先看一个属性 android:inputType:指定输入法的类型,int 类型,可以用|选择 多个。取值可以参考:android.text.InputType 类。取值包括:text,textUri, phone,number, 等. Android SDK 中有这么一句话“If the given content type is TYPE_NULL then a soft keyboard will not be displayed for this text view”, 先将 EditText 的 InputType 改变为 TYPE_NULL, 输入法就不会弹出.然后再设置监听,

再重新设置它的 InputType.
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; } }); 2.默认弹出。有时候按照需求可能要求默认弹出输入法。方案如下: EditText titleInput = (EditText) findViewById(R.id.create_edit_title); titleInput.setFocusable(true); titleInput.requestFocus(); onFocusChange(titleInput.isFocused()); private void onFocusChange(boolean hasFocus) { final boolean isFocus = hasFocus; (new Handler()).postDelayed(new Runnable() { public void run() { InputMethodManager imm = (InputMethodManager) titleInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); if(isFocus) { imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); } else { imm.hideSoftInputFromWindow(titleInput.getWindowToken(),0); } } }, 100); } 我觉得因为在 Android 的主线程中对 UI 的操作无效,所以必须在 Handler 中实现弹出输入 法的操作。 三。关于焦点和输入法的个人理解

获取焦点是获取焦点,弹输入法是弹输入法。获取焦点后并不一定会弹出输入法,在网上搜 了一圈,主流回答是“还有就是已开启界面就是

focus 的 text 的话有可能也是不行

的,UI 渲染是需要时间的”......
由于对源码不懂,我对这一点也没有个全面的认识。只能将焦点和输入法分成两块来处理。 焦点的打开和关闭特别简单。 焦点的获取: titleInput.setFocusable(true); titleInput.requestFocus(); 焦点的取消: titleInput.setFocusable(false);

四。关于经常调用的处理软键盘的函数如下:<转载>
1、打开输入法窗口: InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

// 接受软键盘输入的编辑文本或其它视图 imm.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED); 2、关闭出入法窗口 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

inputMethodManager.hideSoftInputFromWindow(OpeListActivity.this.getCurrentFocus().g etWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); //接受软键盘输入的编辑文本或其它视图 inputMethodManager .showSoftInput(submitBt,InputMethodManager.SHOW_FORCED); 3、如果输入法打开则关闭,如果没打开则打开 InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); 4、获取输入法打开的状态 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); boolean isOpen=imm.isActive(); isOpen 若返回 true,则表示输入法打开 


Android 的 EditText inputtype
Android 中 inputType 属性在 EditText 输入值时启动的虚拟键盘的风格有着重要的作用。 这也大大的方便的操作。有时需要虚拟键盘只为字符或只为数字。所以 inputType 尤为重 要。 设置文本的类型,用于帮助输入法显示合适的键盘类型。 有如下值设置: none、text、 textCapCharacters 字母大小、 textCapWords 单词首字母大小、

textCapSentences 仅第一个字母大小、 textAutoCorrect、 textAutoComplete 自动完成、 textMultiLine 多行输入、 textImeMultiLine 输入法多行(如果支持)、 textNoSuggestions 不提示、 textEmailAddress 电子邮件地址、 textEmailSubject 邮件主题、 textShortMessage 短信息(会多一个表情按钮出来,点开如下图)、

textLongMessage 长讯息?、 textPersonName 人名、 textPostalAddress 地址、 textPassword 密码、 textVisiblePassword 可见密码(实现禁止输入中文)、 textWebEditText 作为网页表单的文本、 textFilte 文本筛选过滤、 textPhonetic 拼音输入、 numberSigned 有符号数字格式、 numberDecimal 可带小数点的浮点格式、 phone 电话号码、

datetime 时间日期、 date 日期、 time 时间 <EditText android:layout_width="fill_parent" android:layout_height="wrap_content"android:inputType="phone" /> //文本类型,多为大写、小写和数字符号。 android:inputType="none" android:inputType="text" android:inputType="textCapCharacters" android:inputType="textCapWords" android:inputType="textCapSentences" android:inputType="textAutoCorrect" android:inputType="textAutoComplete" android:inputType="textMultiLine" android:inputType="textImeMultiLine" android:inputType="textNoSuggestions" android:inputType="textUri" android:inputType="textEmailAddress" android:inputType="textEmailSubject" android:inputType="textShortMessage" android:inputType="textLongMessage" android:inputType="textPersonName" android:inputType="textPostalAddress" android:inputType="textPassword" android:inputType="textVisiblePassword" android:inputType="textWebEditText" android:inputType="textFilter" android:inputType="textPhonetic" //数值类型 android:inputType="number" android:inputType="numberSigned" android:inputType="numberDecimal" android:inputType="phone"//拨号键盘

android:inputType="datetime" android:inputType="date"//日期键盘 android:inputType="time"//时间键盘

android 软键盘隐藏总结
分类: android2012-04-10 10:26 172 人阅读 评论(0) 收藏 举报 androidservice 输入法 actionnull

1>软键盘的状态——隐藏或显示。 一:自动弹出软键盘

1. Timer timer=new Timer(); 2. 3. 4. 5. public void run() { InputMethodManager inputMethodManager=(InputMethodManager) get SystemService(Context.INPUT_METHOD_SERVICE); 6. inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_N OT_ALWAYS); 7. 8. } }, 2000); timer.schedule(new TimerTask() {

二:软键盘 Activity 中设置:

Android:windowSoftInputMode="stateUnspecified",默认设置: 软键盘的状态 (隐藏或可见)没有被指定。系统将选择一个合适的状态或依赖于主题的设 置。 藏。 "stateUnchanged", 软键盘被 "stateAlwaysHidden", 软键 保持上次的状态。 "stateHidden", 当用户选择该 Activity 时,软键盘被隐

盘总是被隐藏的。 "stateVisible",. 软键盘是可见 的。 "stateAlwaysVisible", 当用 户选择这个 Activity 时,软键盘是可见的。 "adjustUnspecified", . 它不被 指定是否该 Activity 主窗口调整大小以便留出软键盘的空间, 或是否窗口上的 内容得到屏幕上当前的焦点是可见的。 系统将自动选择这些模式中一种主要依赖 于是否窗口的内容有任何布局视图能够滚动他们的内容。 如果有这样的一个视 图, 这个窗口将调整大小, 这样的假设可以使滚动窗口的内容在一个较小的区域 中可见的。这个是主窗口默认的行为设置。也就是说, 系统自动决定是采用平 移模式还是压缩模式,决定因素在于内容是否可以滚 动。 "adjustResize", (压缩模式) 当软键盘弹出时,要对主窗口调整屏幕的大小以便留出软键盘的空间。 "adjustPan"] > (平移模式:当输入框不会被遮挡时,该模式没有对布局进行 调整,然而当输入框将要被遮挡时, 窗口就会进行平移。也就是说,该模式始 终是保持输入框为可见
三:隐藏软键盘: EditText edit=(EditText)findViewById(R.id.edit); InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(edit.getWindowToken(),0);

. . .

4.EditText 默认不弹出软件键盘 方法一: 在 AndroidMainfest.xml 中选择哪个 activity,设置 windowSoftInputMode 属性为 adjustUnspecified|stateHidden 例如:<activity Android:name=".Main" Android:label="@string/app_name" Android:windowSoftInputMode="adjustUnspecified|stateHidden" Android:configChanges="orientation|keyboardHidden"> <intent-filter> <action Android:name="android.intent.action.MAIN" /> <category Android:name="android.intent.category.LAUNCHER" />

</intent-filter> </activity> 方法二: 让 EditText 失去焦点,使用 EditText 的 clearFocus 方法 例如:EditText edit=(EditText)findViewById(R.id.edit); edit.clearFocus(); 方法三: 强制隐藏 Android 输入法窗口 例如:EditText edit=(EditText)findViewById(R.id.edit); InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(edit.getWindowToken(),0); 5.EditText 始终不弹出软件键盘 例:EditText edit=(EditText)findViewById(R.id.edit); edit.setInputType(InputType.TYPE_NULL);

Android 中 EditTex 焦点设置和弹 不弹出输入法的问题
今天编程碰到了一个问题:有一款平板,打开一个有 EditText 的 Activity 会默认弹出输入 法。 为了解决这个问题就深入研究了下 android 中焦点 Focus 和弹出输入法的问题。 在网上看了 些例子都不够全面,在这里全面总结下。 一:EditText 为什么会默认弹出输入法? 同样的代码,碰到有 EditText 控件的界面时有的机子会弹出输入法,有的机子不会弹 出。不好意思,这问题我也一头雾水,谁知道可以告诉我,否则我就把这个问题留下来,以后研 究 android 源码时再搞个清楚。但是...我有解决方案。 二:默认弹出和默认关闭输入法的解决方案。 1.默认关闭,不至于进入 Activity 就打开输入法,影响界面美观。 ①在布局文件中,在 EditText 前面放置一个看不到的 LinearLayout,让他率先获取焦点:

<LinearLayout android:focusable="true" android:focusableInTouchMode="true" android:layout_width="0px" android:layout_height="0px"/>

②方法二:先看一个属性 android:inputType:指定输入法的类型,int 类型,可以用|选择 多个。取值可以参考:android.text.InputType 类。取值包括:text,textUri, phone,number, 等. Android SDK 中有这么一句话“If the given content type is TYPE_NULL then a soft keyboard will not be displayed for this text view”, 先将 EditText 的 InputType 改变为 TYPE_NULL, 输入法就不会弹出.然后再设置监听,

再重新设置它的 InputType.
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; } }); 2.默认弹出。有时候按照需求可能要求默认弹出输入法。方案如下: EditText titleInput = (EditText) findViewById(R.id.create_edit_title); titleInput.setFocusable(true); titleInput.requestFocus(); onFocusChange(titleInput.isFocused()); private void onFocusChange(boolean hasFocus) { final boolean isFocus = hasFocus; (new Handler()).postDelayed(new Runnable() { public void run() { InputMethodManager imm = (InputMethodManager) titleInput.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); if(isFocus) { imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); } else { imm.hideSoftInputFromWindow(titleInput.getWindowToken(),0); } } }, 100); } 我觉得因为在 Android 的主线程中对 UI 的操作无效,所以必须在 Handler 中实现弹出输入 法的操作。 三。关于焦点和输入法的个人理解

获取焦点是获取焦点,弹输入法是弹输入法。获取焦点后并不一定会弹出输入法,在网上搜 了一圈,主流回答是“还有就是已开启界面就是

focus 的 text 的话有可能也是不行

的,UI 渲染是需要时间的”......
由于对源码不懂,我对这一点也没有个全面的认识。只能将焦点和输入法分成两块来处理。 焦点的打开和关闭特别简单。 焦点的获取: titleInput.setFocusable(true); titleInput.requestFocus(); 焦点的取消: titleInput.setFocusable(false);

四。关于经常调用的处理软键盘的函数如下:<转载>
1、打开输入法窗口: InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

// 接受软键盘输入的编辑文本或其它视图 imm.showSoftInput(submitBt,InputMethodManager.SHOW_FORCED); 2、关闭出入法窗口 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

inputMethodManager.hideSoftInputFromWindow(OpeListActivity.this.getCurrentFocus().g etWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); //接受软键盘输入的编辑文本或其它视图 inputMethodManager .showSoftInput(submitBt,InputMethodManager.SHOW_FORCED); 3、如果输入法打开则关闭,如果没打开则打开 InputMethodManager m=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); m.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS); 4、获取输入法打开的状态 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); boolean isOpen=imm.isActive(); isOpen 若返回 true,则表示输入法打开 。


转自 http://lanxicy.com/read/4c2fa276d202815a0c7e6c4d.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值