禁用EditText闪烁光标

问题描述:

有谁知道如何禁用EditText视图中的闪烁光标?禁用EditText闪烁光标

您可以使用xml属性android:cursorVisible="false"或java函数setCursorVisible(false)

+7

真棒,这连同onclicklistener,使得它可见给出了正确的行为:) – Warpzit 2012-04-27 08:59:17

+0

机器人:cursorVisible =“假“(为我工作。谢谢) – 2013-05-15 09:45:41

+0

@cerin有没有办法隐藏蓝色标记,所以我可以禁用粘贴,但保持光标可见,以便用户可以看到他在现场的位置? – limlim 2014-08-21 14:05:57

完美的解决方案进一步朝目标

目标:禁用闪烁光标的时候EditText没有焦点,并启用闪烁的光标时EditText对焦。当点击EditText时,下面也会打开键盘,并在键盘上按完成时隐藏它。

1)设置在你的XML在你EditText

android:cursorVisible="false" 

2)设置onClickListener:

iEditText.setOnClickListener(editTextClickListener); 

OnClickListener editTextClickListener = new OnClickListener() 

{ 

    public void onClick(View v) 
    { 
     if (v.getId() == iEditText.getId()) 
     { 
      iEditText.setCursorVisible(true); 
     } 

    } 
}; 

3)然后onCreate,在完成时使用OnEditorActionListener压到你的EditText捕获事件,然后setCursorVisible(false)

//onCreate... 

iEditText.setOnEditorActionListener(new OnEditorActionListener() { 

      @Override 
      public boolean onEditorAction(TextView v, int actionId, 
        KeyEvent event) { 
        iEditText.setCursorVisible(false); 
       if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) { 
        InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
        in.hideSoftInputFromWindow(iEditText.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS); 
       } 
       return false; 
      } 
     }); 
+5

KeyEvent.KEYCODE_ENTER不适用于所有键盘。您需要在布局中特别提及android:imeOptions =“actionDone”,然后再处理EditorInfo.IME_ACTION_DONE。 – PsyGik 2014-12-01 08:31:39

+0

很好的回答只需要点击触摸取代,因为一些时间编辑文本获得焦点没有点击,所以在这种情况下,光标不会显示 – Tony 2016-02-18 11:26:38

就我而言,我希望当编辑集中启用/禁用光标。

在你的活动:

@Override 
public boolean dispatchTouchEvent(MotionEvent ev) { 
    if (ev.getAction() == MotionEvent.ACTION_DOWN) { 
     View v = getCurrentFocus(); 
     if (v instanceof EditText) { 
      EditText edit = ((EditText) v); 
      Rect outR = new Rect(); 
      edit.getGlobalVisibleRect(outR); 
      Boolean isKeyboardOpen = !outR.contains((int)ev.getRawX(), (int)ev.getRawY()); 
      System.out.print("Is Keyboard? " + isKeyboardOpen); 
      if (isKeyboardOpen) { 
       System.out.print("Entro al IF"); 
       edit.clearFocus(); 
       InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE); 
       imm.hideSoftInputFromWindow(edit.getWindowToken(), 0); 
      } 

      edit.setCursorVisible(!isKeyboardOpen); 

     } 
    } 
    return super.dispatchTouchEvent(ev); 
} 
+0

很确定你想'编辑.setCursorVisible(!isKeyboardOpen);'编辑。 setCursorVisible(isKeyboardOpen);'使键盘出现时光标可见。 – 2017-04-10 18:19:52

您可以使用启用和以编程方式禁用编辑文本光标下面的代码。

启用光标

editText.requestFocus(); 
    editText.setCursorVisible(true); 

禁用光标

editText.setCursorVisible(false); 

使用XML启用禁用光标

android:cursorVisible="false/true" 
    android:focusable="false/true" 

为了使edit_text可选择至(复制/剪切/粘贴/选择/选择所有)

editText.setTextIsSelectable(true); 

把重点放在触摸模式写入以下在XML线

android:focusableInTouchMode="true" 
    android:clickable="true" 
    android:focusable="true" 

编程

editText.requestFocusFromTouch(); 

要明确重点触控模式

editText.clearFocus()