nbo*_*ans 0 android listview listener android-fragments
我有一个片段,包含用于搜索输入的EditText和一个ListView.我有搜索部分工作,但现在我想在用户点击EditText之外的屏幕时关闭键盘.我也想使用click事件(因为它是一个ListView,如果我不使用click事件,用户可能会意外地点击他们不想打开的ListView项目)
我知道如何在一个活动中做到这一点,但对于片段来说似乎有所不同.
到目前为止我尝试过的:
实现View.OnClickListener在片段和实施的onClick这样:
@Override
public void onClick(View v) {
System.out.println("onClick true");
if (!(v instanceof EditText)) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.
INPUT_METHOD_SERVICE);
if (getActivity().getCurrentFocus() != null) {
imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
}
}
}
Run Code Online (Sandbox Code Playgroud)当我点击时,这似乎永远不会发布"onClick true".
覆盖片段活动中的onTouchEvent并实现onTouchEvent:
@Override
public boolean onTouchEvent(final MotionEvent event) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.
INPUT_METHOD_SERVICE);
if (getCurrentFocus() != null) {
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
return true;
}
Run Code Online (Sandbox Code Playgroud)任何帮助将不胜感激.提前致谢.
关闭Softkeyboard的代码如下:
public static void hideSoftKeyboard(Activity activity) {
InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
}
Run Code Online (Sandbox Code Playgroud)
您可以将它放在Utility Class中,或者如果您在活动中定义它,请避开activity参数,或者调用hideSoftKeyboard(this).
您可以编写一个迭代活动中每个View的方法,并检查它是否是EditText的一个实例,如果它没有将setOnTouchListener注册到该组件,那么一切都将落实到位.如果你想知道如何做到这一点,事实上它很简单.这是你做的,你写一个递归方法,如下所示.
public void setupUI(View view) {
//Set up touch listener for non-text box views to hide keyboard.
if(!(view instanceof EditText)) {
view.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
hideSoftKeyboard();
return false;
}
});
}
//If a layout container, iterate over children and seed recursion.
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
View innerView = ((ViewGroup) view).getChildAt(i);
setupUI(innerView);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在SetcontentView()之后使用参数作为视图的id调用此方法,如:
RelativeLayoutPanel android:id="@+id/parent"> ... </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
然后调用`setupUI(findViewById(R.id.parent))
尝试这个,
在xml文件中
android:clickable="true"
android:focusableInTouchMode="true"
Run Code Online (Sandbox Code Playgroud)
创建方法。
public void hideKeyboard(View view) {
InputMethodManager inputMethodManager =(InputMethodManager)getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Run Code Online (Sandbox Code Playgroud)
调用方法。
edittext.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
hideKeyboard(v);
}
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5253 次 |
| 最近记录: |