2011-02-10 7 views
0

Ok, c'est sur le point de me rendre fou. Je suis toujours un débutant (et peut-être un idiot) mais celui-ci m'a fait tourner en rond. J'essaie de mettre en œuvre le contrôle du curseur dans un editText via les boutons contextuels (gauche, droite, haut). D'après ce que j'ai recherché, il semble que j'ai besoin des méthodes de l'objet Selection comme moveUp(), moveLeft(), extendUp() etc. mais ils ont tous besoin d'un objet "text.layout" et je ne sais pas comment pour en avoir un.Contrôle du curseur dans EditText

Suis-je sur la bonne voie? Aidez-moi?

Répondre

0

Il semble que vous vouliez utiliser un objet DynamicLayout. (Il est l'une des classes dérivées de text.Layout, un autre étant StaticLayout.)

+0

Comment puis-je obtenir un? Y en a-t-il un dans l'editText ou dois-je en créer un d'une manière ou d'une autre? – Vic

+0

Eh bien, vous pouvez utiliser 'EditText.getLayout()'. Je ne sais pas si cela retournera un DynamicLayout, mais je * pense * qu'il le fera. (Il est possible que cela renvoie un autre type de mise en page, mais qui reste utile pour vos besoins.) Sinon, vous pouvez en construire un en utilisant l'un des trois constructeurs décrits dans la documentation. Je remarque que chacun d'entre eux nécessite une TextPaint, que vous pouvez obtenir en appelant 'EditText.getPaint()'. –

+1

getLayout()! C'est le morceau qui manquait! Merci. J'oublie toujours de parcourir les méthodes héritées (je sais, idiot, mais j'apprends). – Vic

1

Un exemple simple pour déplacer le curseur de texte:

package connect1.de; 


import android.app.Activity; 
import android.os.Bundle; 


import android.text.Editable; 
import android.text.method.KeyListener; 
import android.util.Log; 
import android.view.View; 
import android.view.inputmethod.BaseInputConnection; 
import android.view.inputmethod.CompletionInfo; 
import android.view.inputmethod.ExtractedText; 
import android.view.inputmethod.ExtractedTextRequest; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.view.View.OnClickListener; 


public class Connection1 extends Activity implements OnClickListener 
{ 
    /** Called when the activity is first created. */ 

    private EditText s1 = null; 
    private EditText s2 = null; 
    Button button1; 
    Button button2; 
    private EditableInputConnection in; 
    private test1 t1; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // dieser Aufruf funktioniert nicht !! 
     // in = new EditableInputConnection(s1); 

     setContentView(R.layout.main); 
     // so hunktioniert der Aufruf ohne Absturz 
     s1 = (EditText) findViewById(R.id.editText1); 
     // s2 = (EditText) findViewById(R.id.editText2); 
     s1.setText("Test Button"); 
     button1 = (Button) findViewById(R.id.button1); 
     button2 = (Button) findViewById(R.id.button2); 
     // t1 = new test1(s1); 
     in = new EditableInputConnection(s1); 

     // dies bewirkt ein absolutes Verschieben des Textcursors auf Position 10 - 1, 1 zeigt ans Ende der Eingabe 
     // in.commitText("", 5); 
     // in = new EditableInputConnection(s1); 
     // in.commitText("", 7); 
     button1.setOnClickListener(this); 
     button2.setOnClickListener(this); 
     // in.cursor_pos(); 
    } 

    /* 
    * Copyright (C) 2007-2008 The Android Open Source Project 
    * 
    * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    * use this file except in compliance with the License. You may obtain a copy of 
    * the License at 
    * 
    * http://www.apache.org/licenses/LICENSE-2.0 
    * 
    * Unless required by applicable law or agreed to in writing, software 
    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
    * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
    * License for the specific language governing permissions and limitations under 
    * the License. 
    */ 


    /* 
    * Copyright (C) 2007-2008 The Android Open Source Project 
    * 
    * Licensed under the Apache License, Version 2.0 (the "License"); you may not 
    * use this file except in compliance with the License. You may obtain a copy of 
    * the License at 
    * 
    * http://www.apache.org/licenses/LICENSE-2.0 
    * 
    * Unless required by applicable law or agreed to in writing, software 
    * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
    * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
    * License for the specific language governing permissions and limitations under 
    * the License. 
    */ 
    public void onClick(View v) 
    { 
     int pos = 0; 
     pos = in.cursor_pos(); 
     // wie können die verschiedenen Schaltflächen unterschieden werden 
     if (v.findViewById(R.id.button2) == findViewById(R.id.button2)) 
      // s1.setText(v.findViewById(R.id.button2) + ""); 
      pos = pos + 2; 
     // else 
      // s1.setText("Linke Taste gedrückt"); 

     // für links 
     // in.commitText("", pos); 
     // -1 hat auch Wirkung für Linksschritt 
     in.commitText("", pos); 

    } 


    public class test1 
    { 
      private final TextView mTextView; 

      public test1(TextView textview) 
      { 
       mTextView = textview; 
       mTextView.setText("Klasse 1"); 
      } 
    } 
    public class EditableInputConnection extends BaseInputConnection 
    { 
     private static final boolean DEBUG = false; 
     private static final String TAG = "EditableInputConnection"; 

     private final TextView mTextView; 

     public EditableInputConnection(TextView textview) 
     { 
      super(textview, true); 
      mTextView = textview; 
      // mTextView.setText("Inputconnection"); 
     } 

     public Editable getEditable() 
     { 
      TextView tv = mTextView; 
      if (tv != null) 
      { 
       return tv.getEditableText(); 
      } 
      return null; 
     } 

     public boolean beginBatchEdit() 
     { 
      mTextView.beginBatchEdit(); 
      return true; 
     } 

     public boolean endBatchEdit() 
     { 
      mTextView.endBatchEdit(); 
      return true; 
     } 

     public boolean clearMetaKeyStates(int states) 
     { 
      final Editable content = getEditable(); 
      if (content == null) return false; 
      KeyListener kl = mTextView.getKeyListener(); 
      if (kl != null) { 
       try { 
        kl.clearMetaKeyState(mTextView, content, states); 
       } catch (AbstractMethodError e) { 
        // This is an old listener that doesn't implement the 
        // new method. 
       } 
      } 
      return true; 
     } 

     public boolean commitCompletion(CompletionInfo text) 
     { 
      if (DEBUG) Log.v(TAG, "commitCompletion " + text); 
      mTextView.beginBatchEdit(); 
      mTextView.onCommitCompletion(text); 
      mTextView.endBatchEdit(); 
      return true; 
     } 

     public boolean performEditorAction(int actionCode) 
     { 
      if (DEBUG) Log.v(TAG, "performEditorAction " + actionCode); 
      mTextView.onEditorAction(actionCode); 
      return true; 
     } 

     public boolean performContextMenuAction(int id) 
     { 
      if (DEBUG) Log.v(TAG, "performContextMenuAction " + id); 
      mTextView.beginBatchEdit(); 
      mTextView.onTextContextMenuItem(id); 
      mTextView.endBatchEdit(); 
      return true; 
     } 
     public int cursor_pos() 
     { 
      int anzahl = 0; 
      CharSequence str ; 
      str = super.getTextBeforeCursor(s1.length(), 0); 
      // s2.setText(str.length()+ ""); 
      anzahl = str.length(); 
      return anzahl; 
     } 
     public ExtractedText getExtractedText(ExtractedTextRequest request, int flags) 
     { 
      if (mTextView != null) 
      { 
       ExtractedText et = new ExtractedText(); 
       if (mTextView.extractText(request, et)) { 
        if ((flags&GET_EXTRACTED_TEXT_MONITOR) != 0) 
        { 
         // dieser Aufruf funktioniert nicht !! 
         // mTextView.setExtracting(request); 
        } 
        return et; 
       } 
      } 
      return null; 
     } 

     public boolean performPrivateCommand(String action, Bundle data) 
     { 
      mTextView.onPrivateIMECommand(action, data); 
      return true; 
     } 

     @Override 
     public boolean commitText(CharSequence text, int newCursorPosition) 
     { 
      // notwendig, da sonst Eingabetext für Inputmethode nicht aktualisiert wird!! 
      mTextView.setText(s1.getText()); 
      if (mTextView == null) 
      { 
       return super.commitText(text, newCursorPosition); 
      } 
      CharSequence errorBefore = mTextView.getError(); 
      // mTextView.setText(""); 

      boolean success = super.commitText(text, newCursorPosition); 
      CharSequence errorAfter = mTextView.getError(); 

      if (errorAfter != null && errorBefore == errorAfter) 
      { 
       mTextView.setError(null, null); 
      } 

      // s2.setText(newCursorPosition + ""); 
      return success; 

     } 
    } 

} 
Questions connexes