KEYCODE_DEL删除文本视图中的文字标题

时间:2016-12-02 06:23:32

标签: android textview

我希望三个字符保留在textview中,而KEYCODE_DEL必须提前感谢发生 在主要活动:

     text = (AutoCompleteTextView)findViewById(R.id.textbox);
     *********************************************************
     @Override
     public boolean onKeyUp(int keyCode,KeyEvent event) {

      if (event.getAction() == KeyEvent.ACTION_UP
           && keyCode == KeyEvent.KEYCODE_DEL) {

           //some code

           return true;

       }
       return super.onKeyUp(keyCode,event);
   }

2 个答案:

答案 0 :(得分:1)

如果您要求删除每个字符,请尝试以下代码

ediTextView.setOnKeyListener(new View.OnKeyListener()
    {
     @Override
     public boolean onKey (View v,int keyCode, KeyEvent event){
        return backPressAction(keyCode);
       }
    });

    private boolean backPressAction(int keyCode) {
        if (keyCode == KeyEvent.KEYCODE_DEL) {
            int keyPressedPosition = mPasswordView.getSelectionStart();
            if (keyPressedPosition > 3) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

答案 1 :(得分:-1)

最后使用textwatcher

        text.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            if(cs.toString().length() == 0)
                text.setText(" ");

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) { }

        @Override
        public void afterTextChanged(Editable arg0) { }

    });