2012-12-10 16 views
6

Próbuję zdefiniować pole EditText bez automatycznego wyświetlania klawiatury po dotknięciu tego pola. Muszę też mieć wyświetlany migający kursor i poruszać się w oparciu o dotyk. Jest to łatwe do zrobienia przed Androidem 4.0, po prostu używając mText.setInputType (InputType.TYPE_NULL). Jest to jedyny sposób na wyłączenie automatycznego wyświetlania klawiatury programowej, ale w systemie Android 4.0 eliminuje również migający kursor. Kursor jednak ustawia się prawidłowo, a metoda mText.getSelectionStart() zwraca ostatnią lokalizację dotyku. Na przykład, jeśli dotknę między "2" i "3" w polu EditText zawierającym "123", funkcja mText.getSelectionStart() poprawnie zwróci wartość 2, mimo że żaden kursor nie jest wyświetlany. Czy istnieje sposób programowego wyświetlania kursora w tej lokalizacji?Android 4.0 EditText bez miękkiej klawiatury i pozycjonowania kursora

Oto kod używam do testowania EditText lokalizacji kursora:

public class TestCodeActivity extends Activity implements OnClickListener { 
     private EditText mText; 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
       getApplicationContext(); 
       setContentView(R.layout.test); 
       findViewById(R.id.Button01).setOnClickListener(this); 
       findViewById(R.id.Button02).setOnClickListener(this); 
       findViewById(R.id.Button03).setOnClickListener(this); 
       findViewById(R.id.Button04).setOnClickListener(this); 
       mText = (EditText) findViewById(R.id.editText1); 
       mText.setInputType(InputType.TYPE_NULL); 
     } 

     @Override 
     public void onClick(View v) { 
      if (v.getTag().equals("Clear")) { 
       mText.setText(""); 
      } else { 
       String buttontag = v.getTag().toString(); 
       String str = mText.getText().toString(); 
       int cursor = mText.getSelectionStart(); 
       if(cursor==str.length()) 
        str = str + buttontag; 
       else 
        str = str.substring(0, cursor) + buttontag + str.substring(cursor, str.length()); 
       mText.setText(str); 
       mText.setSelection(cursor+1); 
       // ---> need code to display blinking cursor at cursor+1 location ??? 
     } 
     } 
    } 

Oto xml układ:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="85dp" > 
     <EditText 
      android:id="@+id/editText1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="0.13" 
      android:ems="10" 
      android:textSize="25sp" > 
      <requestFocus /> 
     </EditText> 
    </LinearLayout> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
     <Button 
      android:id="@+id/Button01" 
      android:layout_width="60dp" 
      android:layout_height="60dp" 
      android:text="1" 
      android:tag="1" 
      android:layout_gravity="center" 
      android:textSize="30sp" /> 
     <Button 
      android:id="@+id/Button02" 
      android:layout_width="60dp" 
      android:layout_height="60dp" 
      android:text="2" 
      android:tag="2" 
      android:layout_gravity="center" 
      android:textSize="30sp" /> 
     <Button 
      android:id="@+id/Button03" 
      android:layout_width="60dp" 
      android:layout_height="60dp" 
      android:text="3" 
      android:tag="3"       
      android:layout_gravity="center" 
      android:textSize="30sp" /> 
     <Button 
      android:id="@+id/Button04" 
      android:layout_width="60dp" 
      android:layout_height="60dp" 
      android:text="Clear" 
      android:tag="Clear"       
      android:layout_gravity="center" 
      android:textSize="30sp" /> 
    </LinearLayout> 

</LinearLayout> 

Odpowiedz

13

I w końcu zorientowali się, jak stłumić klawiatury ekranowej i wciąż migający kursor do wyświetlenia w polu EditText. Zakodowałem moduł onTouchListener i zwróciłem wartość true, aby wyłączyć klawiaturę zamiast "mText.setInputType (InputType.TYPE_NULL)". Następnie musiałem uzyskać pozycję dotykową, aby ustawić kursor w odpowiednim miejscu.

Oto kod użyłem:

mText = (EditText) findViewById(R.id.editText1); 
    OnTouchListener otl = new OnTouchListener() { 
    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
      Layout layout = ((EditText) v).getLayout(); 
      float x = event.getX() + mText.getScrollX(); 
      int offset = layout.getOffsetForHorizontal(0, x); 
      if(offset>0) 
       if(x>layout.getLineMax(0)) 
        mText.setSelection(offset);  // touch was at end of text 
       else 
        mText.setSelection(offset - 1); 
      break; 
      } 
     return true; 
     } 
    }; 
    mText.setOnTouchListener(otl); 
+0

uratowałeś mój dzień z tego rozwiązania! +1 –

+0

to rozwiązanie nie działa w przypadku edycji wielowierszowej. Czy możesz powiedzieć, jak to zrobić w edytorze wielowierszowym? – user1213202

+0

@ user1213202: Skoncentruj się na 'int offset = layout.getOffsetForHorizontal (0, x);', pierwszy parametr metody Layout.getOffsetForHorizontal (int, float horiz) jest indeksem linii twojego editekstu, dla edytora wielowierszowego , musisz określić indeks linii, na którym chcesz przesunąć kursor, a następnie przekazać go do 'getOffsetForHorizontal'. – nautilusvn

Powiązane problemy