【问题标题】:how to restore the edittext cursor position to end after requesting focus请求焦点后如何将edittext光标位置恢复为结束
【发布时间】:2012-02-28 02:16:01
【问题描述】:

当我多次请求一个editext的焦点时,即使有一些文本,它也会将光标移动到开头。我们怎样才能把光标放在已经存在的文本之后??

【问题讨论】:

标签: android


【解决方案1】:

这会将光标设置到文本的最后一个位置:

editText.setSelection(editText.getText().length());

【讨论】:

    【解决方案2】:

    Edit Text 有一个叫做 append 的属性。我认为下面这行代码也可以达到目的。

    editText.append("");
    

    【讨论】:

      【解决方案3】:

      在 Layout EditText 中将 android:ellipsize 属性作为结尾,就像这样 android:ellipsize="end"

      希望这会有所帮助

      【讨论】:

        【解决方案4】:

        我有一个类似的要求,当用户想要编辑并点击 EditText 时,光标应该在最后。我使用以下方法实现了相同的效果:

        edtDisplayName.setOnTouchListener(new OnTouchListener() {
        
                @Override
                public boolean onTouch(View v, MotionEvent event) {
        
                    if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_DOWN) {
        
                        edtDisplayName.setSelection(edtDisplayName.getText()
                                .length());
        
                        edtDisplayName.requestFocus();
        
                        //Open key board for user to edit
                        Commons.showKeyBoard(OnBoardActivity.this, edtDisplayName);
        
                        return true;
        
                    }
        
                    return false;
                }
            });
        

        我是这样打开键盘的:

        public static void showKeyBoard(Context context, View view) {
            InputMethodManager imm = (InputMethodManager) context
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(view, 0);
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-12-26
          • 2014-04-14
          • 2014-04-27
          • 1970-01-01
          • 2023-02-10
          • 2023-03-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多