2012-12-10 45 views
6

我試圖定義一個EditText框,而沒有軟鍵盤顯示自動當框被觸摸。我還需要讓閃爍的光標顯示並基於觸摸移動。通過使用mText.setInputType(InputType.TYPE_NULL),在Android 4.0之前執行操作很簡單。這是抑制自動軟鍵盤顯示的唯一方法,但在Android 4.0中,它也抑制了閃爍的光標。但是,光標的位置正確,並且mText.getSelectionStart()確實會返回最後一個觸摸位置。例如,如果我在包含「123」的EditText框中的「2」和「3」之間觸摸,即使沒有顯示光標,mText.getSelectionStart()也會返回2。有沒有辦法在該位置以編程方式顯示光標?Android 4.0 EditText沒有軟鍵盤和光標定位

這裏是我用來測試的EditText光標位置代碼:

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

這裏是佈局的xml:

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

回答

13

我終於想通了如何抑制軟鍵盤,仍然獲得一個閃爍的光標顯示在EditText框中。我編寫了一個onTouchListener,並返回true來禁用鍵盤,而不是使用「mText.setInputType(InputType.TYPE_NULL)」。然後我必須得到觸摸位置來將光標設置到正確的位置。

這裏是我使用的代碼:

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

你救了我的一天,這個解決方案! +1 –

+0

此解決方案不適用於多行編輯文本。請告訴您如何處理多行編輯文本? – user1213202

+0

@ user1213202:請關注'int offset = layout.getOffsetForHorizo​​ntal(0,x);',方法Layout.getOffsetForHorizo​​ntal(int line,float horiz)的第一個參數是edittext中的行索引,用於多行edittext ,您需要確定要將光標移動到的行索引,然後將其傳遞到'getOffsetForHorizo​​ntal'。 – nautilusvn