如何在不使用OnkeyListner的情况下检测ENTER

时间:2013-10-28 08:35:36

标签: android android-edittext

我遇到了关键监听器的问题,我在这里已经完全解释了Unable to get line number on press of ENTER

现在,如果我使用textwatcher在我的edittext上实现某些功能,那么每当光标移动到下一行时,我都需要行号。我想知道何时按下ENTER键,我该怎么做

这是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                          
                }
             }
         });

2 个答案:

答案 0 :(得分:1)

试试这个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);
            }
        }
    }
};

答案 1 :(得分:0)

您可以为OnKeyListener添加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;
}