Android 4.0的EditText上没有软键盘和鼠标定位鼠标、键盘、Android、EditText

2023-09-06 09:41:45 作者:资深狗友i

我试图定义一个EditText框而不具有自动软键盘显示时,框感动。我还需要有显示闪烁的光标和基于触摸移动。这是简单的事情之前的Andr​​oid 4.0,只需使用mText.setInputType(InputType.TYPE_NULL)。这是晚饭preSS自动软键盘显示的唯一方式,但在Android 4.0的它也燮presses闪烁的光标。光标确实,但是,位置正常,mText.getSelectionStart()不返回最后一个触摸位置。举例来说,如果我在包含一个EditText中的2和3之间的触碰123,mText.getSelectionStart()正确返回,即使不显示光标2。有没有办法在那个位置以编程方式显示光标?

下面是code我用来测试的EditText光标位置:

 公共类测试codeActivity延伸活动实现OnClickListener {
        私人的EditText行文字;
        @覆盖
        公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
                getApplicationContext();
                的setContentView(R.layout.test);
                findViewById(R.id.Button01).setOnClickListener(本);
                findViewById(R.id.Button02).setOnClickListener(本);
                findViewById(R.id.Button03).setOnClickListener(本);
                findViewById(R.id.Button04).setOnClickListener(本);
                多行文字=(EditText上)findViewById(R.id.editText1);
                mText.setInputType(InputType.TYPE_NULL);
        }

        @覆盖
        公共无效的onClick(视图v){
            如果(v.getTag()。等于(清除)){
                mText.setText();
            } 其他 {
                。字符串buttontag = v.getTag()的toString();
                。字符串str = mText.getText()的toString();
                INT光标= mText.getSelectionStart();
                如果(光标== str.length())
                    海峡= STR + buttontag;
                其他
                    海峡= str.substring(0,光标)+ buttontag + str.substring(光标,str.length());
                mText.setText(STR);
                mText.setSelection(光标+ 1);
                // --->需要code,显示闪烁的光标,在光标+ 1的位置???
        }
        }
    }
 

下面是布局的xml:

 < XML版本=1.0编码=UTF-8&GT?; < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:方向=垂直>

    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =85dp>
        <的EditText
            机器人:ID =@ + ID / editText1
            机器人:layout_width =0dp
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_weight =0.13
            机器人:EMS =10
            机器人:TEXTSIZE =25sp>
            <不是requestFocus />
        < /的EditText>
    < / LinearLayout中>

    <的LinearLayout
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT>
        <按钮
            机器人:ID =@ + ID / Button01
            机器人:layout_width =60dp
            机器人:layout_height =60dp
            机器人:文本=1
            机器人:标签=1
            机器人:layout_gravity =中心
            机器人:TEXTSIZE =30sp/>
        <按钮
            机器人:ID =@ + ID / Button02
            机器人:layout_width =60dp
            机器人:layout_height =60dp
            机器人:文本=2
            机器人:标签=2
            机器人:layout_gravity =中心
            机器人:TEXTSIZE =30sp/>
        <按钮
            机器人:ID =@ + ID / Button03
            机器人:layout_width =60dp
            机器人:layout_height =60dp
            机器人:文本=3
            机器人:标签=3
            机器人:layout_gravity =中心
            机器人:TEXTSIZE =30sp/>
        <按钮
            机器人:ID =@ + ID / Button04
            机器人:layout_width =60dp
            机器人:layout_height =60dp
            机器人:文本=清除
            机器人:标签=清除
            机器人:layout_gravity =中心
            机器人:TEXTSIZE =30sp/>
    < / LinearLayout中>

< / LinearLayout中>
 
Android软键盘的显示与隐藏

解决方案

我终于想通了如何晚饭preSS软键盘,仍然可以得到一个闪烁的光标在一个EditText框中显示。我codeD的onTouchListener,回到真正的禁用,而不是使用mText.setInputType(InputType.TYPE_NULL)的键盘。然后我得触摸位置来设置光标到正确的位置。

下面是code我用:

 多行文字=(EditText上)findViewById(R.id.editText1);
  OnTouchListener OTL =新OnTouchListener(){
  @覆盖
  公共布尔onTouch(视图V,MotionEvent事件){
      开关(event.getAction()){
      案例MotionEvent.ACTION_DOWN:
          布局布局=((EditText上)V).getLayout();
          浮X = event.getX()+ mText.getScrollX();
          的int偏移= layout.getOffsetForHorizo​​ntal(0,x)的;
          如果(偏移0)
              如果(X GT; layout.getLineMax(0))
                  mText.setSelection(偏移); //触摸在文本的结尾
              其他
                  mText.setSelection(偏移 -  1);
          打破;
          }
      返回true;
      }
  };
  mText.setOnTouchListener(OTL);
 

I am attempting to define an EditText box without having the soft keyboard display automatically when the box is touched. 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). This is the only way to suppress automatic soft keyboard display but in Android 4.0 it also suppresses the blinking cursor. The cursor does, however, position properly and mText.getSelectionStart() does return the last touch location. 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. Is there a way to programmatically display a cursor at that location?

Here is the code I am using to test EditText cursor locations:

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 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. I coded an onTouchListener and returned true to disable the keyboard instead of using "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);