Android AlertDialog 对话框开启和关闭输入法问题

Android AlertDialog 对话框开启和关闭输入法问题

AlertDialog 对话框是android jar包自带的对话框,说实话 真的很丑,黑白相间,不是很推荐使用。大多时候 特别是项目中,不建议使用,通常的方法都是自定义对话框,封装一个顶级对话框,通过继承的方式 再去实现相应优美的对话框,整体效果界面很好,且易于管理和维护。因为今天主要讨论一下对话框弹出和关闭输入法问题,这个对自定义也是通用的,所以就AlertDialog 为例。

一:基本格式

new AlertDialog.Builder(self)
    .setTitle("请重新命名")
    .setIcon(android.R.drawable.ic_dialog_info)
    .setView(new EditText(self))
    .setPositiveButton("确定", null)
    .setNegativeButton("取消", null)
    .show();

这里写图片描述

这个是其中的一个格式,可以通过设置不同的item来控制不同的样式,但是大体的风格就如同上面一样。由于涉及到了 EditText 所以就有输入法弹出框。

这里写图片描述

二:输入法弹出框
当然并不是都可以顺利打开。输入法框面临两个问题。
1.输入法无法打开情况:
可以阅读官方文档:http://developer.android.com/reference/android/app/Dialog.html
其中有一段:
Note: Activities provide a facility to manage the creation, saving and restoring of dialogs. See onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog(int), anddismissDialog(int). If these methods are used, getOwnerActivity() will return the Activity that managed this dialog.

Often you will want to have a Dialog display on top of the current input method, because there is no reason for it to accept text. You can do this by setting theWindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM window flag (assuming your Dialog takes input focus, as it the default) with the following code:

 getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
         WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

这是默认情况下隐藏软键盘的方法,要重新显示软键盘,要执行下面这段代码:

alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

AlertDialog.setView()则不会出现以上问题。
另外:为了防止弹出输入法时把后面的背景挤变形,可以在Manifest里添加:

Android:windowSoftInputMode="adjustPan|stateHidden"

2.输入法无法关闭的问题:
问题1网上有很多,问题2的话则很难搜到。因为很多时候我们并不是在主 Activity中处理 我们可能在一个 view Fragment 中处理时候需要注意 。

private void close hideSoftInput(){
InputMethodManager inputMethodManager = (InputMethodManager)               getSystemService(Context.INPUT_METHOD_SERVICE); 
inputMethodManager.hideSoftInputFromWindow(FieldActivity.this.getCurrentFocus().getWindowToken(),  InputMethodManager.HIDE_NOT_ALWAYS);
}

当我们在 确定 或者 取消 事件后 我们希望 对话框消失的时候 ,输入法弹出框也消失,事实上 我们只需要处理输入法,不然手动关闭输入法真的很蹩脚。

FieldActivity.this.getCurrentFocus().getWindowToken()=null
,当我们在事件中调用的时候,无法获取指定依附视图的令牌,程序崩溃掉了,如果能预先try catch一下也好,但是并不能关闭输入法。

正确的姿势是,传入指定视图,获取指定的令牌。 由于我们并不都是主线程中操作,涉及UI的问题,我们需要post到主线程,当然处理方式很多。比图 post到EditText的线程,context.runOnUiThread(Runable runabe) ,Handler ,asynctask等方式。

我们只需要在 确定或者取消的事件里面执行如下:

context.runOnUiThread(new Runable(){
 public void run(){
    hideSoftInput(editText);
    }
  });
private void close hideSoftInput(EditText editText){
InputMethodManager inputMethodManager = (InputMethodManager)               getSystemService(Context.INPUT_METHOD_SERVICE); 
inputMethodManager.hideSoftInputFromWindow(editText.getWindowToken(),  InputMethodManager.HIDE_NOT_ALWAYS);
}

editText.getWindowToken()
这样就可以成功关闭输入法。

完毕。补充一个小地方吧。如果输入框第一次点击时候需要显示默认的参数,如果不做处理的话,EditText 的光标会在最前面 不在字儿后面。
需要在EditText初始化的时候加上;这样光标就在末尾处。

editText.setSelection(editText.getText().length());

“`

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值