Android Edit Text - Cursor stays at the starting position

前端 未结 5 1108
野趣味
野趣味 2021-01-19 03:03

I am using an Edit Text in my project.

The problem is that whenever I type anything into the text box, it shows up, but the cursor does not move from its starting po

5条回答
  •  离开以前
    2021-01-19 03:29

    I am using this workaround for this problem, If characters are in English or digit characters then change EditText gravity to left else in another Language in my case it's in Arabic then will change gravity to left and the cursor will move in it's right way according to each language:

            editText.addTextChangedListener(new TextWatcher() {
    
                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    if(s.length() > 0){
                        char c = s.charAt(0);
                         // characters are Enlgish or digits.
                        if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || Character.isDigit(c)) {
                            editText.setGravity(Gravity.LEFT);
                        }else {
                            editText.setGravity(Gravity.RIGHT);
    
    
                        }
                    } 
                }
    
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
    
                }
    
                @Override
                public void afterTextChanged(Editable s) {
    
                }
            });
    

提交回复
热议问题