2012-12-10 44 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