How can I make the soft keyboard invalid when an EditText is focused

257 views Asked by At

When I click an EditText, the soft keyboard appears. I don't want the keyboard to appear when I clicked an EditText, however, I want to get EditText's forces. I want to edit it without the keyboard.

editText.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        InputMethodManager imm = (InputMethodManager) 
        getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(),
        InputMethodManager.HIDE_NOT_ALWAYS);
    }
});

In this code, the keyboard doesn't appear when I click once. But the keyboard appears when I click many times successively.

How can I improve it? Please tell me how to do it. Thank you.

1

There are 1 answers

12
Charuක On BEST ANSWER

Key :

onCheckIsTextEditor() Check whether the called view is a text editor, in which case it would make sense to automatically display a soft input window for it. This Returns true if this view is a text editor. So let's change that! Then keyboard does not appear :)

Create your own class

import android.content.Context;
import android.util.AttributeSet;
import android.widget.EditText;


public class NoImeEditText extends EditText {
    public NoImeEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    public boolean onCheckIsTextEditor() {
        return false;
    }
}

set your xml like this

  <your.package.name.NoImeEditText
         android:textSize="17dp"
         android:textColor="#FFF"
         android:id="@+id/edit_query"
         android:layout_width="match_parent"
         android:layout_height="60dp" />

call it

 editText = (NoImeEditText) findViewById(R.id.edit_query);

check focus

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
                      // show your custom key pad and do your work 
                    Toast.makeText(getApplicationContext(), "got the focus", Toast.LENGTH_LONG).show();
                }else {
                    Toast.makeText(getApplicationContext(), "lost the focus", Toast.LENGTH_LONG).show();
                }
            }
        });