代码之家  ›  专栏  ›  技术社区  ›  A.Turner

当用户单击“从其他位置编辑文本”(使用片段)时隐藏键盘?[副本]

  •  1
  • A.Turner  · 技术社区  · 7 年前

    当用户离开键盘(页面上的任何位置)点击时,它就会关闭(可以在没有键盘的情况下查看页面,因为我在页面上有一个键编辑他们可能想要填写的文本…)我在堆栈溢出问题上尝试了很多解决方案和答案,但似乎没有对我有用。。。

    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,但都失败了。。。

    任何帮助都会很好,谢谢! 艾比

    3 回复  |  直到 7 年前
        1
  •  2
  •   Sachin Rajput KgaboL    7 年前

    您可以使用 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之外的任何位置单击时,它都会为您隐藏软键盘。

        2
  •  1
  •   Rahul Singh Chandrabhan    7 年前

    将此添加到父活动:

    @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"
    

    希望有帮助。

        3
  •  0
  •   Adil    7 年前

    你可以这样试试

     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();
            }
        }
    

    像这样使用

    forceHideKeyboard(getActivity(),editText);