2012-08-14 65 views
1

我想在java代碼中設置我的EditText,這樣當我點擊時只會顯示Cursor而不是android標準鍵盤。我怎樣才能做到這一點? 感謝當我點擊EditText時,如何僅顯示光標而不顯示標準Android鍵盤?

我編輯:

我的EditText的數組:

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

而且這種方法:

@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); 
      } 
     } 
    } 
} 

,但這種方式不工作,我仍然看到標準鍵盤

回答

-1
add android:configChanges="keyboardHidden" in manifestfile to the activity 
+0

我試過這個解決方案但不起作用 – user1480020 2012-08-14 10:58:41

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;  
    }   
} 

因爲這是我的第三次嘗試,我開始感到愚蠢,我個人測試這次,它的工作。如果您使用佈局,請不要忘記更改佈局中的類型。

+0

我試過這個解決方案但是不起作用 – user1480020 2012-08-14 10:57:29

+0

請參閱這個解決方案然後:http://stackoverflow.com/questions/4841476/hiding-the-android-keyboard-for-edittext/10399125#10399125 – 2012-08-14 11:12:21

+0

不起作用:-( – user1480020 2012-08-14 11:58:52

0

要將其應用於一個EditText(不適用於Activity如何告知其他答案),只需在focusChangeListener中提供隱藏即可。

mEditText.setOnFocusChangeListener(new CustomListener()); 

和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和隱藏,只有當它是真的。

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); 
    } 
} 
0

從@Lupsaa here最好的解決辦法:

旗textIsSelectable設置爲true,禁用軟鍵盤。

你可以這樣設置它在你的XML佈局:

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

或以編程方式,就像這樣:

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

editText.setTextIsSelectable(真);

光標仍然存在,您可以選擇/複製/剪切/粘貼,但軟鍵盤永遠不會顯示。

相關問題