我有一个 MultiAutoCompleteTextView 自定义控件,当用户按下空格键时,我正在其中创建筹码文本。

当文本框为空时,我不希望用户最初输入空格,因此我放置了一个 inputFilter 以防止用户最初输入空格。

这是过滤器代码:

private void RestrictUselessSpaces(){
    InputFilter filter = new InputFilter() {

        public CharSequence filter(CharSequence source, int start, int end,
Spanned dest, int dstart, int dend) {

            for (int i = start; i < end; i++) {
                if (Character.isWhitespace(source.charAt(i))) {

                    if(!getText().toString().trim().equals(""))
                    {
                        return " ";
                    }
                    else
                    {
                        return "";
                    }
                }
        }
        return null;
        }
};

    setFilters(new InputFilter[]{filter});
}

不知何故,当我评论 RestrictUselessSpaces 函数时。它工作正常。但是当这个函数运行时。比如果我尝试在一个字符后输入空格。它给了我 indexoutofboundexception 错误。这是我收到错误的代码。
public void setChips(String s){

    if(s.contains(" ") && !s.trim().equals("")) // check space in string
    {
        this.chips = s.trim().split(" ");

        SpannableStringBuilder ssb = new SpannableStringBuilder(getText());
        // split string wich comma
        int x =0;
        // loop will generate ImageSpan for every country name separated by comma
        for(String c : chips){
            // inflate chips_edittext layout
            LayoutInflater lf = (LayoutInflater) getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            TextView textView = (TextView) lf.inflate(R.layout.chips_edittext, null);
            textView.setText(c); // set text


            setFlags(textView, c); // set flag image
            // capture bitmapt of genreated textview
            int spec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
            textView.measure(spec, spec);
            textView.layout(0, 0, textView.getMeasuredWidth(), textView.getMeasuredHeight());
            Bitmap b = Bitmap.createBitmap(textView.getWidth(), textView.getHeight(),Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(b);
            canvas.translate(-textView.getScrollX(), -textView.getScrollY());
            textView.draw(canvas);
            textView.setDrawingCacheEnabled(true);
            Bitmap cacheBmp = textView.getDrawingCache();
            Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
            textView.destroyDrawingCache();  // destory drawable
            // create bitmap drawable for imagespan
            BitmapDrawable bmpDrawable = new BitmapDrawable(viewBmp);
            bmpDrawable.setBounds(0, 0,bmpDrawable.getIntrinsicWidth(),bmpDrawable.getIntrinsicHeight());
            // create and set imagespan
            ssb.setSpan(new ImageSpan(bmpDrawable),x ,x + c.length() , Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            x = x+ c.length() +1;
        }

        // set chips span
        setText(ssb);


        // move cursor to last
        setSelection(s.length());
    }

}
ssb.setSpan 行导致了问题,但是当我调试代码时。调试器运行这一行而不给我任何异常。执行此函数后发生错误,这使我更难确定究竟是什么导致了问题。

这是我的 logcat:
08-10 20:04:19.253: E/AndroidRuntime(18461): FATAL EXCEPTION: main
08-10 20:04:19.253: E/AndroidRuntime(18461): Process: com.example.twitter, PID: 18461
08-10 20:04:19.253: E/AndroidRuntime(18461): java.lang.IndexOutOfBoundsException: setSpan (2 ... 2) ends beyond length 1
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1016)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:592)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.text.Selection.setSelection(Selection.java:76)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.text.Selection.setSelection(Selection.java:87)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.widget.EditText.setSelection(EditText.java:94)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at com.example.libraries.ChipsMultiAutoCompleteTextview.setChips(ChipsMultiAutoCompleteTextview.java:281)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at com.example.libraries.ChipsMultiAutoCompleteTextview$1.onTextChanged(ChipsMultiAutoCompleteTextview.java:192)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.widget.TextView.sendOnTextChanged(TextView.java:7429)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.widget.TextView.handleTextChanged(TextView.java:7488)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:9203)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:962)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:496)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:675)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.view.inputmethod.BaseInputConnection.commitText(BaseInputConnection.java:197)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at com.android.internal.widget.EditableInputConnection.commitText(EditableInputConnection.java:183)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:279)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:77)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.os.Handler.dispatchMessage(Handler.java:102)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.os.Looper.loop(Looper.java:212)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at android.app.ActivityThread.main(ActivityThread.java:5151)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at java.lang.reflect.Method.invokeNative(Native Method)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at java.lang.reflect.Method.invoke(Method.java:515)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:877)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
08-10 20:04:19.253: E/AndroidRuntime(18461):    at dalvik.system.NativeStart.main(Native Method)

最佳答案

尝试使用这个

setSelection(s.length() - 1);

关于java - indexoutofboundexception : setSpan (2. ..2) 结束超过长度 1,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25230003/

10-13 09:48