2011-10-05 7 views
6

Mi código es:carácter Inserte entre la posición del cursor en la edición de texto

EditText edt 

    edt.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void afterTextChanged(Editable arg0) { 

      final String number = edt.getText().toString(); 

      int count = arg0.length(); 
      edt.setSelection(count); 

     } 

        } 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 
      // TODO Auto-generated method stub 

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, 
       int count) { 

      final String number = edt.getText().toString(); 

        } 

Tengo un teclado también .. Cuando hago clic en un número particular en el teclado de marcación, tengo que añadir el número a la corriente cursor Postion .... también .. cuando pulso eliminar también, i necesidad de eliminar el número de la posición actual del cursor ... ayuda de Pls

teclado de marcación imagen

enter image description here

Respuesta

23

Pruebe el siguiente código

int start =editText.getSelectionStart(); //this is to get the the cursor position 
String s = "Some string"; 
editText.getText().insert(start, s); //this will get the text and insert the String s into the current position 

Este es el código para eliminar el texto seleccionado de EditarTexto

int start = t1.getSelectionStart(); //getting cursor starting position 
int end = t1.getSelectionEnd();  //getting cursor ending position 
String selectedStr = t1.getText().toString().substring(start, end); //getting the selected Text 
t1.setText(t1.getText().toString().replace(selectedStr, "")); //replacing the selected text with empty String and setting it to EditText 
+0

@Aju En realidad, la parte de eliminación es un truco que no funciona siempre me gusta, si tiene texto como: "Hola, hola, hola" y quiere eliminar solo el medio "Hola", no funcionará. –

1

Debe implementar OnClickListener para sus botones. Dentro de allí necesitas actualizar el texto del editor. Por ejemplo para el botón de '5':

public void onClick(View v) { 

    String currentText = edt.getText(); 
    edt.setText(currentText + "5"); 
} 
+0

Cómo actualizo el texto del editor. – jennifer

+0

Hay 'getText()' & 'setText()' en la clase 'EditText' – Caner

+0

Deberías usar' edt.append ("5") 'para eso ... –

0
editText.getText().insert(editText.getSelectionStart(), text); 

texto ser su real cadena que está buscando insertar.

1

Para insertar:

String dialled_nos = dialpad_text.getText().toString(); 
    String number = view.getTag().toString(); // 1,2....9,0 
    int start = dialpad_text.getSelectionStart(); 
    dialled_nos = new StringBuilder(dialled_nos).insert(start, number).toString(); 
    dialpad_text.setText(dialled_nos); 
    dialpad_text.setSelection(start+1); // cursor will move to new position 

Para retirar:

String dialled_nos = dialpad_text.getText().toString(); 
    int remove_index_position = dialpad_text.getSelectionStart()-1; 
    StringBuilder dialled_nos_builder = new StringBuilder(dialled_nos); 
    if(remove_index_position>=0) { 
     dialled_nos_builder.deleteCharAt(remove_index_position); 
     dialpad_text.setText(dialled_nos_builder.toString()); 
     dialpad_text.setSelection(remove_index_position); 
    } 
+0

Gracias. ¡Su método de eliminación me ayudó! –

1
etPnr.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) { 
      etPnr.removeTextChangedListener(this); 

      // TODO: If you want to do on the text, apply here 

      if (start + count > etPnr.length()) 
       etPnr.setSelection(etPnr.length()); 
      else 
       etPnr.setSelection(start + count); 

      etPnr.addTextChangedListener(this); 
     } 

     @Override 
     public void afterTextChanged(Editable s) { 
     } 
    }); 

se puede probar

0

Actualmente uso de esta solución:

public static void insertText(String text, EditText editText) 
{ 
    int start = Math.max(editText.getSelectionStart(), 0); 
    int end = Math.max(editText.getSelectionEnd(), 0); 

    editText.getText().replace(Math.min(start, end), Math.max(start, end), text, 0, text.length()); 
    try 
    { 
     editText.setSelection(start + 1); 
    } 
    catch (final IndexOutOfBoundsException e) 
    { 

    } 
} 
Cuestiones relacionadas