本文介绍了如何检测不输入使用OnkeyListner pssed $ P $的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有在关键监听器与一个问题,我已经在这里完全解释<一个href=\"http://stackoverflow.com/questions/19629778/unable-to-get-line-number-on-$p$pss-of-enter\">Unable在ENTER 的preSS获得行号

现在,如果我使用的是textwatcher来实现我的EditText某些功能,我需要的行号,每次当我的光标移动到下一行。我想知道当ENTER键为pressed,我怎么能做到这一点。

下面是textwatcher的code

  scene.addTextChangedListener(新TextWatcher(){
             @覆盖
             公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
                    //这里是你的code
                。strcheck = s.toString()子((s.length() - 1),s.length());                如果(nowUpper)
                     strcheck = strcheck.toUpperCase();
                 否则,如果(nowLower)
                     strcheck = strcheck.toLowerCase();             }
             @覆盖
             公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
                     INT后){
                 // TODO自动生成方法存根
             }
             @覆盖
             公共无效afterTextChanged(编辑S){
                如果(nowUpper){                如果(ⅸ== 1)
                 {
                    如果(strcheck ==\\ n)
                        strcheck =一个;
                        Log.v(strcheck,strcheck);
                     九= 0;
                     scene.setText(scene.getText()的toString()子串(0,scene.length() - 1)+ strcheck);
                     scene.setSelection(scene.getText()长());
                 }
                 其他
                 {
                 九= 1;
                 }
                 // TODO自动生成方法存根
                }
             }
         });


解决方案

试试这个TextWatcher:

  TextWatcher观察家=新TextWatcher(){
    @覆盖
    公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
        checkForNewLine(S,开始,伯爵,新行中输入);
    }    @覆盖
    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数后INT){
        checkForNewLine(S,开始,伯爵,新行删除);
    }    @覆盖
    公共无效afterTextChanged(编辑S){
    }    私人无效checkForNewLine(CharSequence中,诠释开始,诠释计数,弦乐味精){
        诠释年底开始= +计数;
        的for(int i =启动; I&LT; END;我++){
            如果(s.charAt(I)=='\\ n'){
                Log.d(TAG,味精);
            }
        }
    }
};

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                          
                }
             }
         });
解决方案

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

这篇关于如何检测不输入使用OnkeyListner pssed $ P $的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 17:28