无法取得ENTER的preSS行号 [英] Unable to get line number on press of ENTER

查看:134
本文介绍了无法取得ENTER的preSS行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个EditText上ENTER键我想调用一个函数,并转到下一行的preSS,我想有后pressing进入我的当前行的行号。

I have an edittext, on press of ENTER key i want to call a function and go to next line, and i want to have the line number of my current line after pressing 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;
        }

    });

我试图用这个函数来获取行号

And i am trying to get line number using this function

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它将返回异常
请大家帮忙

The problem is, if i do not call edittext.onKeyListner, then it will give the right line number but if i call edittext.onKeyListner it will return exception please help

这是我的logcat

here is my 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)

推荐答案

选择和看辛格运河钥匙我会用 TextWatcher 。这里有一个code片段添加到的onCreate 为多行的EditText (我给id为编辑 )。

Instead of Selection and watching singel keys I would use a TextWatcher. Here's a code snippet to add to onCreate for a multi line EditText (I gave the id "edit").

    ((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当前numbe。你可以用换行检测更加swofisticated,而是要说明如何实现它,这应该是好的。

It logs the current numbe of lines to logcat. You can be more swofisticated with the newline detection, but to show how to implement it, this should be ok.

这篇关于无法取得ENTER的preSS行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆