我有自定义listview
与editText
和编辑edittext
数据点击edittext
帮助显示键盘它工作正常。如何在editText外部点击时隐藏键盘?
我的问题是当我点击edittext
之外的键盘时必须隐藏。
感谢...
我有自定义listview
与editText
和编辑edittext
数据点击edittext
帮助显示键盘它工作正常。如何在editText外部点击时隐藏键盘?
我的问题是当我点击edittext
之外的键盘时必须隐藏。
感谢...
为此,你必须采取onTouchListener上布局文件的父布局。在TouchListener上,当在EditText外单击时,必须编码以隐藏键盘。请遵循XML布局和Java类来解决这个问题,请遵循以下网址。
http://amitthaperandroidquery.blogspot.com/2011/10/remove-keyboard-after-click-outside.html
的一种方法是,你可以设置一个焦点变化监听到的EditText。 当控件失去焦点,您可以通过隐藏Keyborad: -
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
et.setOnFocusChangeListener(新OnFocusChangeListener(){ \t \t \t \t @覆盖 \t \t \t \t公共无效onFocusChange (视图v,布尔hasFocus){ \t \t \t \t \t \t \t \t \t \t Log.d(“onChange”,“focus ==”+ hasFocus); }}); – Archana
当我点击像列表视图之外的edittext然后它不响应 – Archana
edittext不会改变焦点,当用setOnFocusChangeListener点击它外面。它只会在将焦点转换为其他可聚焦的其他内容时才起作用,如另一个编辑文本。 – Karl
您可以通过执行以下步骤实现:
让(你的活动内容视图)的父视图点击和可聚焦通过添加以下属性
android:clickable="true"
android:focusableInTouchMode="true"
执行hideKeyboard()方法
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
最后,设置edittext的onFocusChangeListener。
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
另一种方式类似于接受的答案是继承布局的根视图,并覆盖OnInterceptTouchEvent,隐藏键盘并返回false允许触摸传播如常。
下面的例子是Xamarin但很容易移植到Java:
public class KeyboardHidingScrollView : ScrollView
{
public KeyboardHidingScrollView (Context context) : base (context)
{
}
public KeyboardHidingScrollView (Context context, IAttributeSet attrs) : base (context, attrs)
{
}
public override bool OnInterceptTouchEvent (Android.Views.MotionEvent ev)
{
var methodManager = (InputMethodManager)Context.GetSystemService (Context.InputMethodService);
methodManager.HideSoftInputFromWindow (WindowToken, HideSoftInputFlags.None);
return false;
}
}
如果您需要更多帮助,请查询我的博客网址或发表评论。 –
谢谢你,它很好地工作.. – Archana