简体   繁体   English

Android 4.0 EditText没有软键盘和光标定位

[英]Android 4.0 EditText without soft keyboard and with cursor positioning

I am attempting to define an EditText box without having the soft keyboard display automatically when the box is touched. 我试图定义一个EditText框,而不会在触摸框时自动显示软键盘。 I also need to have the blinking cursor displayed and moved based on touch. 我还需要根据触摸显示和移动闪烁的光标。 This is simple to do prior to Android 4.0 by just using mText.setInputType(InputType.TYPE_NULL). 只需使用mText.setInputType(InputType.TYPE_NULL),在Android 4.0之前就可以轻松完成。 This is the only way to suppress automatic soft keyboard display but in Android 4.0 it also suppresses the blinking cursor. 这是抑制自动软键盘显示的唯一方法,但在Android 4.0中它还可以抑制闪烁的光标。 The cursor does, however, position properly and mText.getSelectionStart() does return the last touch location. 但是,光标确实正确定位,mText.getSelectionStart()确实返回最后一个触摸位置。 For example, if I touch between the "2" and "3" in an EditText box containing "123", mText.getSelectionStart() correctly returns a 2 even though no cursor is displayed. 例如,如果我在包含“123”的EditText框中触摸“2”和“3”之间,则mText.getSelectionStart()正确返回2,即使没有显示任何光标。 Is there a way to programmatically display a cursor at that location? 有没有办法以编程方式在该位置显示光标?

Here is the code I am using to test EditText cursor locations: 这是我用来测试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 ???
        }
        }
    }

Here is the layout xml: 这是布局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>

I finally figured out how to suppress the soft keyboard and still get a blinking cursor to display in an EditText box. 我终于想出了如何压制软键盘并仍然在一个EditText框中显示一个闪烁的光标。 I coded an onTouchListener and returned true to disable the keyboard instead of using "mText.setInputType(InputType.TYPE_NULL)". 我编写了一个onTouchListener并返回true以禁用键盘而不是使用“mText.setInputType(InputType.TYPE_NULL)”。 I then had to get the touch position to set the cursor to the correct spot. 然后,我必须得到触摸位置,将光标设置到正确的位置。

Here is the code I used: 这是我使用的代码:

  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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM