Skip to content Skip to sidebar Skip to footer

Hide Soft Keyboard For Edittext Focus/touch

I am trying to hide the soft keyboard from showing when an EditText either receives focus or a touch event. I want the EditText to be editable - there will be text in the EditText

Solution 1:

I ended up with a tricky solution, it works, but i don't like it.

I have a simple layout with a EditText and a Button. When you press the button I add a "*" to the EditText were the cursor is. You can manipulate the cursor with the finger and the soft keyboard never opens because when the EditText gets focus I hide the keyboard.

<?xml version="1.0" encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><EditTextandroid:id="@+id/myEditText"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Text Here"/><Buttonandroid:id="@+id/myButton"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="Click Me!" /></LinearLayout>

and the code in the Activity:

publicclassMainActivity : Activity, View.IOnTouchListener
{
    protectedoverridevoidOnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Main);

        EditText edt = FindViewById<EditText>(Resource.Id.myEditText);
        edt.SetOnTouchListener(this);

        Button button = FindViewById<Button>(Resource.Id.myButton);
        button.Click += delegate { edt.Text = edt.Text.Insert(edt.SelectionStart, "*"); };
    }

    publicboolOnTouch(View v, MotionEvent e)
    {
        // Pass the event to the edit text to have the blinking cursor.
        v.OnTouchEvent(e);
        // Hide the input.var imm = ((InputMethodManager)v.Context.GetSystemService(Context.InputMethodService));
        imm?.HideSoftInputFromWindow(v.WindowToken, HideSoftInputFlags.None);
        returntrue;
    }
}

Final result

enter image description here

Post a Comment for "Hide Soft Keyboard For Edittext Focus/touch"