按ENTER键无法获取行号

时间:2013-10-28 08:08:17

标签: android android-edittext

我有一个edittext,按下ENTER键我想调用一个函数然后转到下一行,我想按Enter键后得到当前行的行号。

我正在使用edittext.onKeyListner

 scene.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub
            if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                    (keyCode == KeyEvent.KEYCODE_ENTER)) {
                  // Perform action on key press

                String s = scene.getText().toString();
                s= s + "\n";
                scene.setText(s);
                final int lineno = getCurrentCursorLine(scene);
                //Log.v("lineno" , Integer.toString(lineno));

                  return true;
                }
            return false;
        }

    });

我正在尝试使用此功能获取行号

public int getCurrentCursorLine(EditText editText)
{    
    int selectionStart = Selection.getSelectionStart(editText.getText());
    Layout layout = editText.getLayout();

    if (!(selectionStart == -1)) {
        return layout.getLineForOffset(selectionStart);
    }

    return -1;
}

问题是,如果我不调用edittext.onKeyListner,那么它将给出正确的行号,但如果我调用edittext.onKeyListner它将返回异常 请帮忙

这是我的logcat

10-28 13:29:22.997: E/AndroidRuntime(20220): FATAL EXCEPTION: main
10-28 13:29:22.997: E/AndroidRuntime(20220): java.lang.NullPointerException
10-28 13:29:22.997: E/AndroidRuntime(20220):    at com.example.screenwritter.Newscene.getCurrentCursorLine(Newscene.java:150)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at com.example.screenwritter.Newscene$2.onKey(Newscene.java:126)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at android.view.View.dispatchKeyEvent(View.java:5558)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1349)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1349)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1349)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1349)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1884)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1366)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1811)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3331)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2601)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at android.os.Looper.loop(Looper.java:137)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at android.app.ActivityThread.main(ActivityThread.java:4448)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at java.lang.reflect.Method.invokeNative(Native Method)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at java.lang.reflect.Method.invoke(Method.java:511)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590)
10-28 13:29:22.997: E/AndroidRuntime(20220):    at dalvik.system.NativeStart.main(Native Method)

enter image description here

1 个答案:

答案 0 :(得分:0)

我会使用Selection而不是TextWatcher而是看着singel键。以下是为多行onCreate添加EditText的代码段(我将ID设为“编辑”)。

    ((EditText) findViewById(R.id.edit)).addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            int lines = (s.length() > 0)? 1:0;
            for(int i = 0; i < s.length(); i++)
                if(s.charAt(i) == '\n') lines++;
            Log.d("TEST", "lines now: " + lines);
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
        public void afterTextChanged(Editable s) {}
    });

它将当前行数记录到logcat。使用换行检测可以更加精通,但为了展示如何实现它,这应该没问题。