我有两个标签。
当1打开时,我想显示键盘。 当另一个打开时,我想确保键盘已关闭。
以下是我想要显示键盘的选项卡片段的代码(当打开它时,它会自动聚焦用户将用于执行搜索的EditText:
public class AddFoodSearchFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View V = inflater.inflate(R.layout.fragment_add_food_search, container, false);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
return V;
}
}
关闭它的片段:
public class AddFoodFormFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View V = inflater.inflate(R.layout.fragment_add_food_form, container, false);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
return V;
}
}
为什么它没有按照我想要的方式回应?我不明白这有什么困难。
修改
我也可以通过在我的1片段中添加onPause()来做到这一点,但它仍然无效:
public class AddFoodSearchFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View V = inflater.inflate(R.layout.fragment_add_food_search, container, false);
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
return V;
}
@Override
public void onPause() {
super.onPause();
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}
}
答案 0 :(得分:0)
以下是我认为你要做的事情的一个例子:
MyEditor.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
int inType = MyEditor.getInputType(); // backup the input type
MyEditor.setInputType(InputType.TYPE_NULL); // disable soft input
MyEditor.onTouchEvent(event); // call native handler
MyEditor.setInputType(inType); // restore input type
return true; // consume touch even
}
});
您应该专注于这一行并将其实施到您的项目中:
MyEditor.setInputType(InputType.TYPE_NULL); // disable soft input
有关详细信息,请查看此Link...
答案 1 :(得分:0)
以下是100%一致的答案:
public class AddFoodSearchFragment extends Fragment {
EditText search;
InputMethodManager imm;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View V = inflater.inflate(R.layout.fragment_add_food_search, container, false);
search = (EditText) V.findViewById(R.id.search);
imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
return V;
}
@Override
public void onPause() {
super.onPause();
imm.hideSoftInputFromWindow(search.getWindowToken(), 0);
}
}