用户完成编辑后如何从EditText中删除焦点?

时间:2012-01-07 02:16:56

标签: android android-edittext

我的布局上有一个EditText。在用户输入一些文本并点击“完成”键后,我想从中删除闪烁的光标。我已经搜索了StackOverflow并找到了3个对我不起作用的答案。闪烁的光标仍然存在。

private class MyOnKeyListener implements OnKeyListener {
  public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN
    && keyCode == KeyEvent.KEYCODE_ENTER) {
      // FAIL 0
      MyActivity.this.findViewById(R.id.someOtherView).requestFocus();

      // FAIL 1
      InputMethodManager imm = (InputMethodManager)getSystemService(
        Context.INPUT_METHOD_SERVICE
      );
      imm.hideSoftInputFromWindow(v.getWindowToken(), 0);

      // FAIL 2
      MyActivity.this.getWindow().setSoftInputMode(
        WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN
      );

      return true;
    } else {
      return false;
    }
  }
}

8 个答案:

答案 0 :(得分:28)

您可以使用xml属性

android:cursorVisible 

或者您可以使用此方法在代码中执行此操作。

 setCursorVisible(boolean).

答案 1 :(得分:16)

使用以下代码从EditText中删除焦点

editText.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View view, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
                    imm.hideSoftInputFromWindow(URLText.getWindowToken(), 0); 
                    editText.setFocusable(false);
                    editText.setFocusableInTouchMode(true);
                    return true;
                } else {
                    return false;
                }
            }
        });

答案 2 :(得分:6)

这是我的自定义EditText,用于检测键盘是否显示&隐藏键盘时自动删除焦点

/**
 * Created by TheFinestArtist on 9/24/15.
 */
public class KeyboardEditText extends EditText {

    public KeyboardEditText(Context context) {
        super(context);
    }

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

    public KeyboardEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setOnTouchListener(OnTouchListener l) {
        super.setOnTouchListener(l);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (listener != null)
            listener.onStateChanged(this, true);
    }

    @Override
    public boolean onKeyPreIme(int keyCode, @NonNull KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_UP) {
            if (listener != null)
                listener.onStateChanged(this, false);

            // Hide cursor
            setFocusable(false);

            // Set EditText to be focusable again
            setFocusable(true);
            setFocusableInTouchMode(true);
        }
        return super.onKeyPreIme(keyCode, event);
    }

    /**
     * Keyboard Listener
     */
    KeyboardListener listener;

    public void setOnKeyboardListener(KeyboardListener listener) {
        this.listener = listener;
    }

    public interface KeyboardListener {
        void onStateChanged(KeyboardEditText keyboardEditText, boolean showing);
    }
}

答案 3 :(得分:2)

经过几次尝试,这才是最适合我的:

EditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
      @Override
      public boolean onEditorAction(TextView textView, int actionId,
          KeyEvent keyEvent) { //triggered when done editing (as clicked done on keyboard)
        if (actionId == EditorInfo.IME_ACTION_DONE) {
          editText.clearFocus();
        }
        return false;
      }
    });

答案 4 :(得分:1)

只需将焦点设置为完成按钮。

答案 5 :(得分:1)

失败0:

如果元素在触摸模式下无法聚焦,则在某些布局元素上调用.requestFocus()是不够的。如果要将焦点设置为视图或按钮,则必须调用

.setFocusableInTouchMode(true);
首先

或将其设置在.xml

答案 6 :(得分:0)

对于Kotlin用户...我从@Sharone Lev那里借了最好的解决方案,但是我不得不添加一些协程延迟,或者由于某种原因,清除焦点的EditText将阻止键盘退出。

someEditText.setOnEditorActionListener { v, actionId, event ->

         when(actionId){
            EditorInfo.IME_ACTION_DONE->{

                //NOTE:wait few milliseconds before dismissing
                CoroutineScope(Dispatchers.IO).launch {
                    delay(500)
                    CoroutineScope(Dispatchers.Main).launch {
                        someEditText.clearFocus()
                    }
                }
            }
            else->{
                Log.w("TAG", "another action id ${actionId}")
            }
        }

        false
    }

答案 7 :(得分:-1)

如果你不想让它完全可编辑我会说下面的事情;

EditText orgid; 
orgid.setText(user.getOrgId());
orgid.setEnabled(false);