【问题标题】:Android 4.0 EditText without soft keyboard and with cursor positioningAndroid 4.0 EditText 没有软键盘和光标定位
【发布时间】:2012-12-10 23:27:52
【问题描述】:

我正在尝试定义一个 EditText 框,但在触摸框时不会自动显示软键盘。我还需要根据触摸显示和移动闪烁的光标。在 Android 4.0 之前,这很简单,只需使用 mText.setInputType(InputType.TYPE_NULL)。这是抑制自动软键盘显示的唯一方法,但在 Android 4.0 中它也抑制闪烁的光标。但是,光标确实定位正确,并且 mText.getSelectionStart() 确实返回了最后一个触摸位置。例如,如果我在包含“123”的 EditText 框中触摸“2”和“3”之间,即使没有显示光标,mText.getSelectionStart() 也会正确返回 2。有没有办法以编程方式在该位置显示光标?

这是我用来测试 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>

【问题讨论】:

    标签: android android-edittext android-cursor


    【解决方案1】:

    我终于想出了如何抑制软键盘并仍然让闪烁的光标显示在 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);
    

    【讨论】:

    • 此解决方案不适用于多行编辑文本。您能告诉我如何处理多行编辑文本吗?
    • @user1213202: 请关注int offset = layout.getOffsetForHorizontal(0, x);,方法Layout.getOffsetForHorizo​​ntal(int line, float horiz)的第一个参数是你的edittext中的行索引,对于多行edittext,你需要确定要移动光标的行索引,然后将其传递给getOffsetForHorizontal
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    相关资源
    最近更新 更多