【问题标题】:How i to move cursor from one edit text another when keyboard backspace key pressing Android当键盘退格键按下Android时,我如何将光标从一个编辑文本移动到另一个
【发布时间】:2019-01-29 13:23:42
【问题描述】:

我目前正在使用 3 个编辑文本字段并将其光标向前移动 (edittext1--> edittext2--> edittext3)edittext input length==1

我想做它的反向方法(删除文本后向后移动光标 (edittext1) 当我按下键盘退格键时

输出

我的代码是这样的

 public EditText otp1, otp2, otp3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_otp_box);

        otp1 = findViewById(R.id.otp1);
        otp2 = findViewById(R.id.otp2);
        otp3 = findViewById(R.id.otp3);

        otp1.setOnKeyListener(new View.OnKeyListener() {

            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (otp1.getText().length() == 1) {
                    otp2.requestFocus();
                }
                return false;
            }
        });

        otp2.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (otp2.getText().length() == 1) {
                    otp3.requestFocus();
                }
                return false;
            }
        });

        otp3.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (otp3.getText().length() == 1) {
                    hideSoftKeyboard(v);
                    return false;
                }
                return false;
            }
        });

    }

    public void hideSoftKeyboard(View view) {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
    }

xml

 <LinearLayout

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:orientation="horizontal">

            <EditText
                android:id="@+id/otp1"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:background="@drawable/edit_txt_border_color"
                android:gravity="center"
                android:imeOptions="actionNext"
                android:inputType="number"
                android:maxLength="1"
                android:text=""
                android:hint="X"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/otp2"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:background="@drawable/edit_txt_border_color"
                android:gravity="center"
                android:imeOptions="actionNext"
                android:inputType="number"
                android:maxLength="1"
                android:text=""
                android:hint="X"
                android:textSize="20sp" />

            <EditText
                android:id="@+id/otp3"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:layout_weight="1"
                android:background="@drawable/edit_txt_border_color"
                android:gravity="center"
                android:imeOptions="actionNext"
                android:inputType="number"
                android:maxLength="1"
                android:text=""
                android:hint="X"
                android:textSize="20sp" />

        </LinearLayout>

【问题讨论】:

    标签: android events android-softkeyboard


    【解决方案1】:

    您可以在文本更改监听器中使用requestFocus() 方法:

        opt1 = findViewById(R.id.opt1);
        opt1.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 1) {
                    opt2.requestFocus();
                }
            }
        });
    
        opt2 = findViewById(R.id.opt2);
        opt2.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 1) {
                    opt3.requestFocus();
                }
    
                if (s.toString().length() == 0) {
                    opt1.requestFocus();
                }
            }
        });
    
        opt3 = findViewById(R.id.opt3);
        opt3.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 0) {
                    opt2.requestFocus();
                }
            }
        });
    

    【讨论】:

      【解决方案2】:

      如果您的EditText 为空,请使用addTextChangedListener(),而不是将焦点移至上一个EditText

      示例代码

      试试这个

          otp1.addTextChangedListener(new TextWatcher() {
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      
                  if(TextUtils.isEmpty(otp1.getText().toString())){
                      otp1.requestFocus();
                  }else {
                      otp2.requestFocus();
                  }
              }
      
              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
      
              }
      
              @Override
              public void afterTextChanged(Editable s) {
      
              }
          });
          otp3.addTextChangedListener(new TextWatcher() {
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      
                  if(TextUtils.isEmpty(otp2.getText().toString())){
                      otp2.requestFocus();
                  }else {
                      // you logic
                  }
              }
      
              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
      
              }
      
              @Override
              public void afterTextChanged(Editable s) {
      
              }
          });
          otp2.addTextChangedListener(new TextWatcher() {
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
      
                  if(TextUtils.isEmpty(otp2.getText().toString())){
                      otp1.requestFocus();
                  }else {
                      otp3.requestFocus();
                  }
              }
      
              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
      
              }
      
              @Override
              public void afterTextChanged(Editable s) {
      
              }
          });
      

      【讨论】:

      • 感谢您的支持。当我更改 if 和 else 条件时,您的代码正在工作,反之亦然
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      相关资源
      最近更新 更多