2016-01-09 56 views
0

我在活動中有一個EditText小部件,當用戶在鍵入時按下空格時會創建文本芯片。我將結果保存爲逗號分隔值並將其存儲在數據庫中。如何在Android中的EditText中設置字符串的芯片

enter image description here

現在我試圖創建另一個活動從數據庫獲取數據並對其進行編輯。我能夠做的最大功能是在EditText中設置文本。

enter image description here

我卡在這裏。我能夠在用戶鍵入代碼時創建芯片,但不能通過代碼創建芯片。我該怎麼做才能獲得理想的行爲?

我下面的代碼:

edt.setText("Europe USA London Spain"); 
edt.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(final CharSequence s, int arg1, int arg2, int arg3) { 

       String addedString = s.toString(); 
       if (addedString.length() > 0 && addedString.endsWith(" ")) { 
        if (!isTextAdditionInProgress) { 
         // something is added 
         edt.setCursorVisible(true); 
         int startIndex = isContactAddedFromDb ? addedString.length() : edt.getSelectionEnd(); 
         startIndex = startIndex < 1 ? 1 : startIndex; 
         isTextAdditionInProgress = true; 
         addOrCheckSpannable(s, startIndex); 
        } 
        stringLength = addedString.length(); 
       } else { 
        Log.v("", "Doesn't contain space, good to go!"); 
       } 
      } 

      @Override 
      public void beforeTextChanged(CharSequence s, int arg1, int arg2, int arg3) { 
       beforeChangeIndex = edt.getSelectionStart(); 
       changeString = s.toString(); 
      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       if (!isTextDeletedFromTouch && s.toString().length() < changeString.length() && !isTextAdditionInProgress) { 
        String deletedString = ""; 
        try { 
         deletedString = changeString.substring(afterChangeIndex, beforeChangeIndex); 

        } catch (Exception e) { 
        } 
        if (deletedString.length() > 0 && deletedString.contains(",")) 
         deletedString = deletedString.replace(",", ""); 
       } 
      } 
     }); 




public void addOrCheckSpannable(CharSequence s, int startIndex) { 
    boolean checkSpannable = false; 
    String overallString; 
    if (s == null) { 
     checkSpannable = true; 
     s = edt.getText(); 
     startIndex = edt.getSelectionEnd(); 
     startIndex = startIndex < 1 ? 1 : startIndex; 
     // startIndex =-1; 
     overallString = edt.toString(); 
     if (TextUtils.isEmpty(overallString.trim())) 
      return; 
    } else { 
     overallString = s.toString(); 
     startIndex = startIndex - 1; 
    } 

    int spanEnd = 0; 
    for (int i = startIndex - 1; i >= 0; i--) { 
     Character c = overallString.charAt(i); 
     if (c == ' ') { 
      spanEnd = i; 
      break; 
     } 
    } 
    SpannableStringBuilder ssb = new SpannableStringBuilder(s); 
    int cursorCurrentPoint = edt.getSelectionEnd(); 
    boolean addedFromFirst = cursorCurrentPoint < overallString.length(); 

    ClickableSpan[] spans = ssb.getSpans(0, addedFromFirst ? spanEnd : ssb.length(), ClickableSpan.class); 

    boolean someUnknownChange = false; 
    if (spans.length > 0) { 
     ClickableSpan underlineSpan = spans[spans.length - 1]; 
     spanEnd = ssb.getSpanEnd(underlineSpan); 
     // int spanCheck = spanEnd+1; 

     int k = 0; 
     for (int m = 0; m < spans.length; m++) { 
      ClickableSpan someSpan = spans[m]; 

      int end = ssb.getSpanEnd(someSpan); 
      ; 
      if (k < end) 
       k = end; 
     } 

     spanEnd = k; 

     if (spanEnd < overallString.length()) { 
      Character c = overallString.charAt(spanEnd); 
      if (c == ' ') { 
       spanEnd += 1; 
      } else { 
       ssb.insert(spanEnd, " "); 
       spanEnd += 1; 
       startIndex += 1; 
       someUnknownChange = true; 
      } 
     } 
    } 

    if (startIndex > -1 && spanEnd > -1) { 
     if (checkSpannable) { 
      ClickableSpan[] span = someUnknownChange ? ssb.getSpans(spanEnd - 1, startIndex - 1, ClickableSpan.class) : ssb.getSpans(spanEnd, startIndex, ClickableSpan.class); 
      if (span.length > 0) { 
       // has span 
      } else if (startIndex > spanEnd) { 
       // ssb.replace(someUnknownChange?spanEnd-1:spanEnd, 
       // someUnknownChange?startIndex-1:startIndex , ""); 
       ssb.replace(spanEnd, startIndex, ""); 

      } 
      return; 
     } else { 
      // this is to checked whether the user deletes comma and adds 
      // again 
      if ((Math.abs(spanEnd - 1 - startIndex) > 1)) { 
       String userInputString = someUnknownChange ? overallString.substring(spanEnd - 1, startIndex - 1) : overallString.substring(spanEnd, startIndex); 
       String trimString = userInputString.trim(); 
       if (trimString.length() == 0) { 
        ssb.replace(spanEnd, startIndex + 1, ""); 

       } else { 
        if (userInputString.charAt(userInputString.length() - 1) == ' ' && spanEnd - 1 >= 0 && startIndex - 1 >= 0) 
         userInputString = overallString.substring(spanEnd - 1, startIndex - 1); 

        if (checkValidation) { 

         BitmapDrawable bmpDrawable = getBitmapFromText(userInputString); 
         bmpDrawable.setBounds(0, 0, bmpDrawable.getIntrinsicWidth(), bmpDrawable.getIntrinsicHeight()); 
         ssb.setSpan(new ImageSpan(bmpDrawable), spanEnd, startIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
         // mm=phoneNum.getMovementMethod(); 
         edt.setMovementMethod(LinkMovementMethod.getInstance()); 

        } else { 

         BitmapDrawable bmpDrawable = getBitmapFromText(userInputString); 
         bmpDrawable.setBounds(0, 0, bmpDrawable.getIntrinsicWidth(), bmpDrawable.getIntrinsicHeight()); 
         ssb.setSpan(new ImageSpan(bmpDrawable), spanEnd, startIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
         // mm=phoneNum.getMovementMethod(); 
         edt.setMovementMethod(LinkMovementMethod.getInstance()); 

        } 
       } 
      } 
     } 
     setSpannableText(ssb); 
    } 
} 

public void setSpannableText(final Spannable ssb) { 
    new Handler().postDelayed(new Runnable() { 

     @Override 
     public void run() { 

      edt.setText(ssb); 
      edt.setSelection(edt.getText().toString().length()); 
      resetFlags(); 
      if (!ssb.toString().equalsIgnoreCase(" ")) { 
       str = ssb.toString(); 
      } 
     } 
    }, 20); 
} 

public void resetFlags() { 
    isContactAddedFromDb = false; 
    isTextAdditionInProgress = false; 
    checkValidation = true; 
} 


private BitmapDrawable getBitmapFromText(String message) { 
    TextView textView = (TextView) layoutInflater.inflate(R.layout.textview, null); 
    textView.setText(message); 
    BitmapDrawable bitmapDrawable = (BitmapDrawable) extractBitmapFromTextView(textView); 
    return bitmapDrawable; 
} 

@SuppressWarnings("deprecation") 
public static Object extractBitmapFromTextView(View view) { 

    int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); 
    view.measure(spec, spec); 
    view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); 
    Bitmap b = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); 
    Canvas c = new Canvas(b); 
    c.translate(-view.getScrollX(), -view.getScrollY()); 
    view.draw(c); 
    view.setDrawingCacheEnabled(true); 
    Bitmap cacheBmp = view.getDrawingCache(); 
    Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true); 
    view.destroyDrawingCache(); 
    return new BitmapDrawable(viewBmp); 
} 
+0

現在你有什麼要求?你面臨什麼問題? – sasikumar

+0

EditText是EditText的子類,對吧?我不確定你正在使用哪個庫,但它沒有添加芯片的方法嗎? –

+0

例如:http://kpbird.blogspot.com/2013/02/android-chips-edittext-token-edittext_26.html?m=1 –

回答

2

試試這個會的工作, 添加這個方法和變量

int count = 0; 

private void setText(String str) { 

    final String[] names = str.split(" "); 
    count = names.length; 
    isContactAddedFromDb = true; 

    final Handler h = new Handler(); 

    Runnable r = new Runnable() { 

     @Override 
     public void run() { 

      isContactAddedFromDb = true; 

      String s = names[names.length - count]; 
      count--; 
      edt.append(s + " "); 
      if (count > 0) 
       h.postDelayed(this, 50); 
     } 
    }; 
    h.post(r); 

} 

,並呼籲像這樣

setText("Europe USA London Spain"); 

而不是

edt.setText("Europe USA London Spain"); 
+0

謝謝.... Jignesh .. !!! – crickpatel0024

相關問題