动态添加一个复选框到android中的edittext

时间:2014-09-10 05:17:03

标签: android checkbox android-edittext indexoutofboundsexception

我正在点按一下按钮执行这些代码。我试图在edittext中动态添加复选框。该复选框逐行添加。

    RelativeLayout r1;
    Layout lay = et1.getLayout();
    String text = et1.getText().toString();
    int start=0, end;
            for(int tmp=0; tmp<et1.getLineCount(); tmp++)
                {
                        end = lay.getLineEnd(i);
                        CheckBox cb = new CheckBox(getApplicationContext());
                        cb.setText(text.substring(start, end));
                        r1.addView(cb);
                        start = end;
                }

1)它显示如下错误:

08-19 16:05:42.431: E/AndroidRuntime(1965): FATAL EXCEPTION: main
08-19 16:05:42.431: E/AndroidRuntime(1965): java.lang.IndexOutOfBoundsException: 2, 0
08-19 16:05:42.431: E/AndroidRuntime(1965):     at android.text.PackedIntVector.getValue(PackedIntVector.java:70)
08-19 16:05:42.431: E/AndroidRuntime(1965):     at android.text.DynamicLayout.getLineStart(DynamicLayout.java:375)
08-19 16:05:42.431: E/AndroidRuntime(1965):     at android.text.Layout.getLineEnd(Layout.java:823)
08-19 16:05:42.431: E/AndroidRuntime(1965):     at bitcream.my.notepad.Newnote$7.onClick(Newnote.java:168)
08-19 16:05:42.431: E/AndroidRuntime(1965):     at android.view.View.performClick(View.java:2485)
08-19 16:05:42.431: E/AndroidRuntime(1965):     at android.view.View$PerformClick.run(View.java:9080)
08-19 16:05:42.431: E/AndroidRuntime(1965):     at android.os.Handler.handleCallback(Handler.java:587)
08-19 16:05:42.431: E/AndroidRuntime(1965):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-19 16:05:42.431: E/AndroidRuntime(1965):     at android.os.Looper.loop(Looper.java:123)
08-19 16:05:42.431: E/AndroidRuntime(1965):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-19 16:05:42.431: E/AndroidRuntime(1965):     at java.lang.reflect.Method.invokeNative(Native Method)
08-19 16:05:42.431: E/AndroidRuntime(1965):     at java.lang.reflect.Method.invoke(Method.java:507)
08-19 16:05:42.431: E/AndroidRuntime(1965):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-19 16:05:42.431: E/AndroidRuntime(1965):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-19 16:05:42.431: E/AndroidRuntime(1965):     at dalvik.system.NativeStart.main(Native Method)

2)此外,我想将这些复选框添加到edittext中,可能我的代码会将其添加到相对布局中。请建议我如何修复错误以及如何在edittext中添加复选框。提前致谢

0 个答案:

没有答案