How can i detect ENTER pressed without using Onkey

2019-06-13 06:32发布

问题:

I am having a problem with the on key listener, which i have completely explained here Unable to get line number on press of ENTER

Now if i am using a textwatcher to implement certain functionalities on my edittext, i need the line number everytime when my cursor moves to the next line. i want to know when the ENTER key is pressed, how can i do that

here is the code of textwatcher

  scene.addTextChangedListener(new TextWatcher() {          
             @Override
             public void onTextChanged(CharSequence s, int start, int before, int count) {                                   
                    //here is your code
                strcheck = s.toString().substring((s.length()-1),s.length());

                if (nowUpper)
                     strcheck = strcheck.toUpperCase();
                 else if (nowLower)
                     strcheck = strcheck.toLowerCase();

             }                       
             @Override
             public void beforeTextChanged(CharSequence s, int start, int count,
                     int after) {
                 // TODO Auto-generated method stub                          
             }                       
             @Override
             public void afterTextChanged(Editable s) {
                if (nowUpper){ 

                if(ix==1)
                 {
                    if(strcheck=="\n")
                        strcheck="a";
                        Log.v(strcheck, strcheck);
                     ix=0;
                     scene.setText(scene.getText().toString().substring(0,scene.length()-1) + strcheck);
                     scene.setSelection(scene.getText().length());


                 }
                 else
                 {
                 ix=1;
                 }
                 // TODO Auto-generated method stub                          
                }
             }
         });

回答1:

try this TextWatcher:

TextWatcher watcher = new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        checkForNewLine(s, start, count, "new line entered");
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        checkForNewLine(s, start, count, "new line deleted");
    }

    @Override
    public void afterTextChanged(Editable s) {
    }

    private void checkForNewLine(CharSequence s, int start, int count, String msg) { 
        int end = start + count;
        for (int i = start; i < end; i++) {
            if (s.charAt(i) == '\n') {
                Log.d(TAG, msg);
            }
        }
    }
};


回答2:

You can add OnKeyListener for your EditText:

myEdit.setOnKeyListener(new OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_UP
                    && keyCode == KeyEvent.KEYCODE_ENTER) {

                int currentLine = getCurrentLine(myEdit);
            }

            return false;
        }
    });

private int getCurrentLine(EditText editText) {
    int lineNumber = 0;
    String text = editText.getText().toString()
            .substring(0, editText.getSelectionStart());

    for (int i = 0; i < text.length(); i++) {
        if (String.valueOf(text.charAt(i)).equalsIgnoreCase("\n")) {
            lineNumber++;
        }
    }

    return lineNumber;
}