关闭 android 默认键盘 onClick 的 EditText

Closing android default keyboard onClick of EditText

好的!所以这个问题有很多答案。但是 none 它对我有用。也许我在某处出错了。

我想要做的是,我不希望在单击 EditText 时弹出默认的 android 软键盘。

这是我的 xml 文件

Activity_main.xml

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="0dp"
        android:layout_marginTop="0dp"
        android:orientation="horizontal"
        android:focusableInTouchMode="true"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="INR"
            android:textSize="28dp"
            android:layout_marginTop="12dp"
            android:layout_marginLeft="55dp"
            ></TextView>

        <ImageView
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:layout_marginLeft="15dp"
            android:src="@drawable/indiaflag"
            />

        <EditText
            android:id="@+id/secondText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:layout_marginLeft="70dp"
            android:textSize="28dp"
            android:text=""
            />
    </LinearLayout>

这是隐藏键盘的代码,我使用 InputMethodManager 在点击 EditText 时不显示键盘

secondText = (EditText) findViewById(R.id.secondText);
 InputMethodManager imm =  (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(secondText, InputMethodManager.HIDE_NOT_ALWAYS);

我在声明 secondText 之后的 OnCreate 中使用此代码,但这不起作用。我什至尝试了在 setOnClickListener 上使用上述代码的替代方法。但是还是没有成功。

谁能告诉我哪里错了? 提前致谢

添加:

secondText.setInputType(InputType.TYPE_NULL);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(secondText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);

而不是

secondText.setInputType(InputType.TYPE_NULL);

使用

 OnTouchListener otl = new OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          switch (event.getAction()) {
          case MotionEvent.ACTION_DOWN:
              Layout layout = ((EditText) v).getLayout();
              float x = event.getX() + secondText.getScrollX();
              int offset = layout.getOffsetForHorizontal(0, x);
              if(offset>0)
                  if(x>layout.getLineMax(0))
                      secondText.setSelection(offset);     // touch was at end of text
                  else
                      secondText.setSelection(offset - 1);
              break;
              }
          return true;
          }
      };
      secondText.setOnTouchListener(otl);

使用此功能隐藏键盘输入

 public static void hide_keyboard(Activity activity) {
    InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    View view = activity.getCurrentFocus();
    if (view == null) 
        view = new View(activity);
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}