2017-01-09 72 views
0

我想在我的項目中使用this有用的庫點擊時沒有顯示出來,這個簡單的類作爲庫部署EditText鍵盤沒有顯示出來,但使用和點擊<code>EditText</code>鍵盤後發達的EditText

public class AlachiqMakeLink extends EditText { 

    private static final String TAG = AlachiqMakeLink.class.getSimpleName(); 
    private static final String DEFAULT_TEXT_COLOR = "FFFFFF"; 

    private String mParkedText = ""; 
    private boolean mIsBoldParkedText = true; 
    private String mParkedTextColor = DEFAULT_TEXT_COLOR; 
    private String mParkedHintColor = DEFAULT_TEXT_COLOR; 

    private String mText = null; 
    private enum TypingState { 
     Start, Typed 
    } 
    private TypingState mTypingState = TypingState.Start; 

    public AlachiqMakeLink(Context context) { 
     super(context); 
     init(); 
    } 

    public AlachiqMakeLink(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

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

     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.AlachiqMakeLink, defStyleAttr, 0); 

     mParkedText = a.getString(R.styleable.AlachiqMakeLink_parkedText); 
     if (mParkedText == null) { 
      mParkedText = ""; 
     } 

     String hint = a.getString(R.styleable.AlachiqMakeLink_parkedHint); 

     mParkedTextColor = a.getString(R.styleable.AlachiqMakeLink_parkedTextColor); 
     if (mParkedTextColor == null) { 
      mParkedTextColor = AlachiqMakeLink.DEFAULT_TEXT_COLOR; 
     } 

     mParkedHintColor = a.getString(R.styleable.AlachiqMakeLink_parkedHintColor); 
     if (mParkedHintColor == null) { 
      mParkedHintColor = AlachiqMakeLink.DEFAULT_TEXT_COLOR; 
     } 

     mIsBoldParkedText = a.getBoolean(R.styleable.AlachiqMakeLink_parkedTextBold, true); 

     init(); 

     if (hint != null) { 
      setPlaceholderText(hint); 
     } 

     a.recycle(); 
    } 

    private void init() { 
     mText = ""; 
     observeText(); 

     mTypingState = TypingState.Start; 
     addTextChangedListener(new ParkedTextViewWatcher(this)); 
    } 

    public String getParkedText() { 
     return mParkedText; 
    } 

    private int getBeginningPositionOfParkedText() { 
     int position = mText.length() - mParkedText.length(); 
     if (position < 0) { 
      return 0; 
     } 
     return position; 
    } 

    private void goToBeginningOfParkedText() { 
     setSelection(getBeginningPositionOfParkedText()); 
    } 

    private String getTypedText() { 
     if (mText.endsWith(mParkedText)) { 
      return mText.substring(0, getBeginningPositionOfParkedText()); 
     } 
     return mText; 
    } 

    private void setTypedText(String typedText) { 
     mText = typedText; 
     observeText(); 

     textChanged(); 
    } 

    private void setEmptyText() { 
     setTypedText(""); 
    } 

    public void setPlaceholderText(String placeholderText) { 
     Spanned hint = null; 
     String parkedTextColor = reformatColor(mParkedTextColor); 
     String parkedHintColor = reformatColor(mParkedHintColor); 
     if (mIsBoldParkedText) { 
      hint = Html.fromHtml(String.format("<font color=\"#%s\">%s</font><font color=\"#%s\"><b>%s</b></font>", parkedHintColor, placeholderText, parkedTextColor, mParkedText)); 
     } else { 
      hint = Html.fromHtml(String.format("<font color=\"#%s\">%s</font><font color=\"#%s\">%s</font>", parkedHintColor, placeholderText, parkedTextColor, mParkedText)); 
     } 
     super.setHint(hint); 
    } 

    // Call when TypedText is changed 
    private String observeText() { 
     return mText = getTypedText() + mParkedText; 
    } 

    private String reformatColor(String color) { 
     if (color.startsWith("#")) { 
      color = color.substring(1); 
     } 

     if (color.length() > 6) { 
      return color.substring(2); 
     } 
     return color; 
    } 

    private Spanned getHtmlText() { 
     String parkedTextColor = reformatColor(mParkedTextColor); 
     if (mIsBoldParkedText) { 
      return Html.fromHtml(String.format("<font color=\"#%s\">%s</font><font color=\"#%s\"><b>%s</b></font>", parkedTextColor, getTypedText(), parkedTextColor, mParkedText)); 
     } 
     return Html.fromHtml(String.format("<font color=\"#%s\">%s</font>", parkedTextColor, getTypedText() + mParkedText)); 
    } 

    private void textChanged() { 
     switch (mTypingState) { 
      case Start: 
       if (mText.length() <= 0) { 
        return; 
       } 
       setText(getHtmlText(), BufferType.SPANNABLE); 
       goToBeginningOfParkedText(); 

       mTypingState = TypingState.Typed; 

      case Typed: 
       if (mText.equals(mParkedText)) { 
        mTypingState = TypingState.Start; 
        setText(getHtmlText(), BufferType.SPANNABLE); 
        return; 
       } 

       setText(getHtmlText(), BufferType.SPANNABLE); 

       goToBeginningOfParkedText(); 

      default: 
       break; 
     } 
    } 

    private static class ParkedTextViewWatcher implements TextWatcher { 

     private AlachiqMakeLink mAlachiqMakeLink; 
     private boolean   mIsDeleteText; 

     public ParkedTextViewWatcher(AlachiqMakeLink alachiqMakeLink) { 
      this.mAlachiqMakeLink = alachiqMakeLink; 
     } 

     @Override 
     public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 

     @Override 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      if (before > 0) { 
       mIsDeleteText = true; 
      } else { 
       mIsDeleteText = false; 
      } 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      mAlachiqMakeLink.removeTextChangedListener(this); 

      String text = s.toString(); 
      if (mIsDeleteText) { 

       if (text.length() < mAlachiqMakeLink.getParkedText().length()) { 
        mAlachiqMakeLink.setEmptyText(); 
       } else { 
        String parkedText = text.substring(mAlachiqMakeLink.getBeginningPositionOfParkedText() - 1); 
        if (!parkedText.equals(mAlachiqMakeLink.getParkedText())) { 
         mAlachiqMakeLink.setEmptyText(); 
        } else { 
         mAlachiqMakeLink.setTypedText(text); 
        } 
       } 
      } else { 
       mAlachiqMakeLink.setTypedText(text); 
      } 

      mAlachiqMakeLink.addTextChangedListener(this); 
     } 
    } 
} 

回答

1

您可以強制顯示或通過添加切換

InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); 
    if (imm.isActive()){ 
     imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0); // hide 
    } else { 
     imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY); // show 
    } 
隱藏鍵盤