2012-12-10 43 views
6

Tôi đang cố xác định hộp EditText mà không tự động hiển thị bàn phím mềm khi hộp được chạm vào. Tôi cũng cần có con trỏ nhấp nháy được hiển thị và di chuyển dựa trên cảm ứng. Điều này rất đơn giản để làm trước Android 4.0 bằng cách chỉ sử dụng mText.setInputType (InputType.TYPE_NULL). Đây là cách duy nhất để ngăn chặn hiển thị bàn phím mềm tự động nhưng trong Android 4.0 nó cũng ngăn chặn con trỏ nhấp nháy. Con trỏ không, tuy nhiên, vị trí đúng và mText.getSelectionStart() không trả lại vị trí liên lạc cuối cùng. Ví dụ, nếu tôi chạm vào giữa "2" và "3" trong một hộp EditText có chứa "123", mText.getSelectionStart() trả về một cách chính xác 2 mặc dù không có con trỏ nào được hiển thị. Có cách nào để hiển thị con trỏ ở vị trí đó không?Android 4.0 EditText không có bàn phím mềm và có định vị con trỏ

Đây là mã tôi đang sử dụng để kiểm tra EditText địa điểm con trỏ:

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 ??? 
     } 
     } 
    } 

Đây là xml bố trí:

<?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> 

Trả lời

13

cuối cùng tôi đã tìm ra cách để ngăn chặn bàn phím mềm mại và vẫn nhận được một con trỏ nhấp nháy để hiển thị trong hộp EditText. Tôi đã mã hóa một onTouchListener và trả về true để vô hiệu hóa bàn phím thay vì sử dụng "mText.setInputType (InputType.TYPE_NULL)". Sau đó tôi đã có được vị trí cảm ứng để đặt con trỏ đến vị trí chính xác.

Đây là mã tôi đã sử dụng:

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

bạn đã lưu trong ngày của tôi với giải pháp này! +1 –

+0

giải pháp này không hoạt động đối với văn bản chỉnh sửa đa dòng. Bạn có thể cho biết cách thực hiện cho edittext nhiều dòng không? – user1213202

+0

@ user1213202: Hãy tập trung vào 'int offset = layout.getOffsetForHorizontal (0, x);', tham số đầu tiên của phương thức Layout.getOffsetForHorizontal (int line, float horiz) là chỉ mục dòng trong edittext của bạn, cho đa dòng edittext , bạn cần phải xác định chỉ mục dòng mà bạn muốn di chuyển con trỏ trên và sau đó chuyển nó vào 'getOffsetForHorizontal'. – nautilusvn

Các vấn đề liên quan