1

I want to set, in java code, my EditText so that, when i click will display only the Cursor but not the android standard keyboard. How can I do this? Thanks

I EDITED:

I have an array of EditText:

for (i=0;i<N*N;i++) {
    value[i].setOnTouchListener(this);
    value[i].setOnClickListener(this);
    value[i].setOnFocusChangeListener(this);
}

and this method:

@Override
public void onClick(View v) {
    for (i =0 ; i < N*N; i++) {

        if(v == value[i]) {
        variable = 1;
        if(jey!=i) {
            jey=i;
            showDialog(CUSTOM_DIALOG);
            }
        }
    }
}

@Override
public void onTouch(View v, MotionEvent event) {
    for (i =0 ; i < N*N; i++) {

        if(v == value[i]) {
        variable = 1;
        if(jey!=i) {
            jey=i;
            showDialog(CUSTOM_DIALOG);
            }
        }
    }
}

@Override
public void onFocusChange(View v, boolean hasFocus) { 
    for (i =0 ; i < N*N; i++){
        if(v == value[i]) {
            variable =1;
            if(jey!=i) {
                jey=i;
                imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(value[jey].getWindowToken(), 0);
            }
        }
    }
}

but in this way does not work I still see the standard keyboard

Chilledrat
  • 2,593
  • 3
  • 28
  • 38
user1480020
  • 143
  • 1
  • 3
  • 10

6 Answers6

0

Or use this:

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Edit:

This is a common solution I found on other threads:

public class NoImeEditText extends EditText {

     public NoImeEditText(Context context, AttributeSet attrs) { 
          super(context, attrs);     
     }  

     @Override      
     public boolean onCheckIsTextEditor() {   
          return false;     
     }        
} 

Since this is my third try and I'm starting to feel stupid, I personnaly tested this time, and it works. Don't forget to also change the type in your layout, if you use one.

Michel-F. Portzert
  • 1,785
  • 13
  • 16
0

To apply it for one EditText (not for Activity how tell other answers), just provide hiding in focusChangeListener.

mEditText.setOnFocusChangeListener(new CustomListener());

and CustomListener:

protected class CustomListener implements View.OnFocusChangeListener {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
}

You can check hasFocus and hide only if it's true.

JaLoveAst1k
  • 512
  • 3
  • 14
0

Here you go.

/**
 * EditText which suppresses IME show up.
 */
public class DigitsEditText extends EditText {
    public DigitsEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        setInputType(getInputType() | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        final InputMethodManager imm = ((InputMethodManager) getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE));
        if (imm != null && imm.isActive(this)) {
            imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final boolean ret = super.onTouchEvent(event);
        // Must be done after super.onTouchEvent()
        final InputMethodManager imm = ((InputMethodManager) getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE));
        if (imm != null && imm.isActive(this)) {
            imm.hideSoftInputFromWindow(getApplicationWindowToken(), 0);
        }
        return ret;
    }

    @Override
    public void sendAccessibilityEventUnchecked(AccessibilityEvent event) {
        if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED) {
            // Since we're replacing the text every time we add or remove a
            // character, only read the difference. (issue 5337550)
            final int added = event.getAddedCount();
            final int removed = event.getRemovedCount();
            final int length = event.getBeforeText().length();
            if (added > removed) {
                event.setRemovedCount(0);
                event.setAddedCount(1);
                event.setFromIndex(length);
            } else if (removed > added) {
                event.setRemovedCount(1);
                event.setAddedCount(0);
                event.setFromIndex(length - 1);
            } else {
                return;
            }
        } else if (event.getEventType() == AccessibilityEvent.TYPE_VIEW_FOCUSED) {
            // The parent EditText class lets tts read "edit box" when this View has a focus, which
            // confuses users on app launch (issue 5275935).
            return;
        }
        super.sendAccessibilityEventUnchecked(event);
    }
}
Tudor Luca
  • 6,259
  • 2
  • 29
  • 44
0

Best solution from @Lupsaa here:

Setting the flag textIsSelectable to true disables the soft keyboard.

You can set it in your xml layout like this:

<EditText
android:id="@+id/editText"
...
android:textIsSelectable="true"/>

Or programmatically, like this:

EditText editText = (EditText) findViewById(R.id.editText);

editText.setTextIsSelectable(true);

The cursor will still be present, you'll be able to select/copy/cut/paste but the soft keyboard will never show.

Community
  • 1
  • 1
Santacrab
  • 3,165
  • 2
  • 25
  • 31
0
public void hideSoftKeyboard() {
    if(getCurrentFocus()!=null) {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    }
}

public void showSoftKeyboard(View view) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
view.requestFocus();
inputMethodManager.showSoftInput(view, 0);
}

When you want to show keyboard, just call (showSoftKeyboard()) and when you want to hide keyboard, just call (hideSoftKeyboard()).

Then, just Add another same size layout on edittext and click event on it,then after you unable to click on edittext.

               <EditText
                    android:id="@+id/etAnswer"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@android:color/transparent"
                    android:cursorVisible="false"
                    android:inputType="number"
                    android:maxLength="6"
                    android:maxLines="1"
                    android:paddingLeft="10dp"
                    android:singleLine="true"
                    android:textSize="20sp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:onClick="onClick" />
Community
  • 1
  • 1
EAS
  • 366
  • 2
  • 18
-1
add android:configChanges="keyboardHidden" in manifestfile to the activity