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

4

6 回答 6

0

或者使用这个:

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

编辑:

这是我在其他线程上找到的常见解决方案:

public class NoImeEditText extends EditText {

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

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

由于这是我的第三次尝试并且我开始感到愚蠢,所以这次我亲自进行了测试,并且有效。如果您使用一种,请不要忘记更改布局中的类型。

于 2012-08-14T10:54:46.387 回答
0

要将它应用于一个 EditText(而不是 Activity 如何告诉其他答案),只需在 focusChangeListener 中提供隐藏。

mEditText.setOnFocusChangeListener(new 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);
    }
}

hasFocus只有当它是真的时,你才能检查和隐藏。

于 2012-08-14T10:59:24.300 回答
0

干得好。

/**
 * 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);
    }
}
于 2012-12-19T19:25:17.213 回答
0

@Lupsaa 的最佳解决方案在这里

将标志 textIsSelectable 设置为 true 会禁用软键盘。

您可以像这样在 xml 布局中设置它:

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

或以编程方式,如下所示:

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

editText.setTextIsSelectable(true);

光标仍然存在,您将能够选择/复制/剪切/粘贴,但软键盘永远不会显示。

于 2015-04-08T08:22:25.497 回答
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);
}

当你想显示键盘时,只需调用(showSoftKeyboard()),当你想隐藏键盘时,只需调用(hideSoftKeyboard())

然后,只需在edittext上添加另一个相同大小的布局并在其上单击事件,然后在您无法单击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" />
于 2019-11-29T06:18:53.230 回答
-1
add android:configChanges="keyboardHidden" in manifestfile to the activity
于 2012-08-14T10:52:37.457 回答