删除[反向退格]按钮

时间:2014-06-05 11:26:33

标签: java android android-edittext android-button

我已经构建了一个删除按钮,就像在Windows中一样,删除光标右侧的字符串(逐个)。 [而不是删除左侧的主流Backspace。]

但是,当右侧为空时,onClickListener应显示Toast消息。

但应用程序崩溃了。

这就是我所做的:

package com.reverse.app;

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView.BufferType;
import android.widget.Toast;

public class MainActivity extends Activity {
    public Toast t;
    Editable a, b, d, f, a1;
    String e;
    public int c, d1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText ed;

        Button dlt;

        dlt = (Button) findViewById(R.id.button1);

        ed = (EditText) findViewById(R.id.editText1);

        dlt.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {

                a = ed.getEditableText();

                b = a;
                c = ed.getSelectionStart();

                boolean daj = b.toString().isEmpty();
            if (!daj) {

                    String a22 = b.toString().substring(0, c);
                    int strChar = a11.length();
                    String strcut = a11.substring(1, strChar);
                    e = a22.concat(strcut);

                    ed.setText(e, BufferType.EDITABLE);
                    b = ed.getEditableText();
                    ed.setSelection(c);
                } else {
                    Toast.makeText(getApplicationContext(), "Cool",
                            t.LENGTH_SHORT).show();
                }

            }

        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

Logcat:

06-05 17:03:41.394: E/AndroidRuntime(967): FATAL EXCEPTION: main
06-05 17:03:41.394: E/AndroidRuntime(967): java.lang.StringIndexOutOfBoundsException: length=0; regionStart=1; regionLength=-1
06-05 17:03:41.394: E/AndroidRuntime(967):  at java.lang.String.startEndAndLength(String.java:583)
06-05 17:03:41.394: E/AndroidRuntime(967):  at java.lang.String.substring(String.java:1464)
06-05 17:03:41.394: E/AndroidRuntime(967):  at com.example.app.MainActivity$1.onClick(MainActivity.java:46)
06-05 17:03:41.394: E/AndroidRuntime(967):  at android.view.View.performClick(View.java:4476)
06-05 17:03:41.394: E/AndroidRuntime(967):  at android.view.View$PerformClick.run(View.java:18795)
06-05 17:03:41.394: E/AndroidRuntime(967):  at android.os.Handler.handleCallback(Handler.java:730)
06-05 17:03:41.394: E/AndroidRuntime(967):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 17:03:41.394: E/AndroidRuntime(967):  at android.os.Looper.loop(Looper.java:176)
06-05 17:03:41.394: E/AndroidRuntime(967):  at android.app.ActivityThread.main(ActivityThread.java:5493)
06-05 17:03:41.394: E/AndroidRuntime(967):  at java.lang.reflect.Method.invokeNative(Native Method)
06-05 17:03:41.394: E/AndroidRuntime(967):  at java.lang.reflect.Method.invoke(Method.java:525)
06-05 17:03:41.394: E/AndroidRuntime(967):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
06-05 17:03:41.394: E/AndroidRuntime(967):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
06-05 17:03:41.394: E/AndroidRuntime(967):  at dalvik.system.NativeStart.main(Native Method)

如果editText中的文本右侧(字符串a11)为null or "",那为什么它会进入if循环?为什么不继续else声明?

2 个答案:

答案 0 :(得分:0)

您在以下代码段中将起始索引硬编码为1。如果要访问其索引,则字符串长度应大于1.检查

String strcut = a11.substring(1, strChar);

如果你想要上面的代码,那么你的字符串长度应该至少为2;#34;不要崩溃"。

答案 1 :(得分:0)

java.lang.StringIndexOutOfBoundsException:length=0;regionStart=1;regionLength=-1

检查您的 a11发生了同样的事情

    String a="";<---Value in a11
    System.out.println(a==null);--->False
    System.out.println(a.length());---->0
    System.out.println(a.substring(1,0));--->String index out of range: -1

因此,请检查您的a11

if(a11!=null && !a11.trim().equals(""))