我试图这样做,当用户点击键盘(页面上的任何位置)时它会关闭(为了能够在没有它的情况下查看页面,因为我在页面上有他们可能需要的关键编辑文本填写之前..)我已经尝试了很多解决方案和堆栈溢出的许多答案,但似乎没有为我工作...
public class OneFragment extends Fragment implements BlockingStep {
public interface PassingData { //create interface to pass data
void passData (String data);
}
String stepOne;
EditText _fromET;
EditText _toET;
EditText _refET;
EditText _numET;
private PassingData PD_listener; // declare member variable of interface
@Override
public void onAttach(Context context) {
super.onAttach(context);
PD_listener = (PassingData) context; //initialize interface using onAttach method
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragmentone, container, false);
_fromET = (EditText) view.findViewById(R.id.FROM);
_toET = (EditText) view.findViewById(R.id.TO);
_refET = (EditText) view.findViewById(R.id.REF_NUM);
_numET = (EditText) view.findViewById(R.id.CONTACTNUM);
return view;
}
我尝试使用inputmethodmanager和focusListener,但都失败了......
任何帮助都会很棒,谢谢! 阿比
答案 0 :(得分:2)
您可以使用 OnFocusChangeListener
yourEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);
}
});
你需要在你的EditText上使用它,每当你在EditText的任何地方点击它时,它会为你隐藏SoftKeyboard。
答案 1 :(得分:1)
将此添加到您的父活动:
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if ( v instanceof EditText) {
Rect outRect = new Rect();
v.getGlobalVisibleRect(outRect);
if (!outRect.contains((int)event.getRawX(), (int)event.getRawY())) {
v.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
}
}
return super.dispatchTouchEvent(event);
}
将其添加到片段的布局中:
android:focusableInTouchMode="true"
希望它有所帮助。
答案 2 :(得分:0)
你可以这样试试
i
像这样使用
public static void forceHideKeyboard(AppCompatActivity activity, EditText editText) {
try {
if (activity.getCurrentFocus() == null
|| !(activity.getCurrentFocus() instanceof EditText)) {
editText.requestFocus();
}
InputMethodManager imm = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
activity.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}