我想在点击按钮时在编辑文本中显示芯片。这是我尝试过的:
ChipsMultiAutoCompleteTextview.java (这个类有它的结构和这个方法,当用户点击按钮时将名称设置为芯片。)
public void setChips(String chip_title) {
if (!chip_title.equals("")) {
SpannableStringBuilder ssb = new SpannableStringBuilder(getText());
int x = 0;
LayoutInflater lf = (LayoutInflater) getContext().getSystemService(
Activity.LAYOUT_INFLATER_SERVICE);
TextView textView = (TextView) lf.inflate(R.layout.chips_edittext,
null);
textView.setText(chip_title); // set text
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());
Log.d("startx", "" + x);
Log.d("end", "" + (x + chip_title.length()));
// create and set imagespan
ssb.setSpan(new ImageSpan(bmpDrawable), x, x + chip_title.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
x = x + chip_title.length() + 1;
setText(ssb);
// move cursor to last
setSelection(getText().length());
}
}
MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
ChipsMultiAutoCompleteTextview mu;
Button btn1, btn2, btn3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mu = (ChipsMultiAutoCompleteTextview) findViewById(R.id.multiAutoCompleteTextView1);
btn1 = (Button) findViewById(R.id.but1);
btn1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.but1:
String str1 = btn1.getText().toString();
Log.d("Button name", str1);
ChipsMultiAutoCompleteTextview chip1 = new ChipsMultiAutoCompleteTextview(
this);
chip1.setChips(str1);
break;
}
}
}
程序在单击按钮时应在编辑文本中生成芯片。但是,我收到此错误。 logcat详细信息如下:
04-12 10:34:01.730: E/AndroidRuntime(23041): FATAL EXCEPTION: main
04-12 10:34:01.730: E/AndroidRuntime(23041): java.lang.IndexOutOfBoundsException: setSpan (0 ... 4) ends beyond length 0
04-12 10:34:01.730: E/AndroidRuntime(23041): at android.text.SpannableStringBuilder.checkRange(SpannableStringBuilder.java:1016)
04-12 10:34:01.730: E/AndroidRuntime(23041): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:592)
04-12 10:34:01.730: E/AndroidRuntime(23041): at android.text.SpannableStringBuilder.setSpan(SpannableStringBuilder.java:588)
04-12 10:34:01.730: E/AndroidRuntime(23041): at com.androidchipps.ChipsMultiAutoCompleteTextview.setChips(ChipsMultiAutoCompleteTextview.java:109)
04-12 10:34:01.730: E/AndroidRuntime(23041): at com.androidchipps.MainActivity.onClick(MainActivity.java:65)