2016-09-20 4 views
3

의 끝 부분에 커서를 설정하는 방법 글고 이미 텍스트가있는 경우 커서가 end.Even에 있어야하며 왼쪽 상내가 글고 치기를 터치 할 때마다 글고

editTextView.setOnTouchListener(new View.OnTouchListener() { 
       @Override 
       public boolean onTouch(View v, MotionEvent event) { 

        editTextView.onTouchEvent(event); 

        if(!textFocus) { 
         editTextView.setSelection(editTextView.getText().length()); 
         textFocus = true; 
        } 

        return true; 
       } 
      }); 

      editTextView.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
       @Override 
       public void onFocusChange(View v, boolean hasFocus) { 
        textFocus = false; 
       } 
      }); 

답변

2

당신을 이동하는 것을 허용해서는 안 EditText를 확장하고 onSelectionChanged 함수를 재정의 할 수 있습니다. 예를 들어

: XML에서

public class CustomEditText extends EditText { 
     public CustomEditText(Context context, AttributeSet attrs) { 
      super(context, attrs); 
     } 

     public CustomEditText(Context context) { 
      super(context); 
     } 

     public CustomEditText(Context context, AttributeSet attrs, int defStyle) { 
      super(context, attrs, defStyle); 
     } 

     @Override 
     protected void onSelectionChanged(int selStart, int selEnd) { 
       this.setSelection(this.getText().length()); 
     } 
    } 

당신은 다음 yourpackagename.CustomEditText 대신 글고 치기를 사용해야합니다.

0

초점 변경 또는

editText.setText(editText.getText().toString); 

,이 작업을 수행 할 수 있습니다에 실제로 직접이 작업을 수행하는 그러한 방법이 없다, 그것은

a) 

    editText.setText("Your text"); 
    editText.setSelection(editText.getText().length()); 
b) 

    editText.setText("");  
    editText.append("Your text"); 

c) 

    editText.append(""); 
0

작동합니다 코드 아래 중 하나를 시도하지만, 이제까지하고 싶어.

5

이렇게 도움이되었습니다. edittext를 터치하면 항상 커서가 끝납니다.

editTextView.setOnTouchListener(new View.OnTouchListener() { 
       @Override 
       public boolean onTouch(View v, MotionEvent event) { 
        editTextView.onTouchEvent(event); 
        editTextView.setSelection(editTextView.getText().length()); 
        return true; 
       } 
      });