Android Custom EditText not showing cursor in ICS

白昼怎懂夜的黑 提交于 2019-12-04 04:26:33

why don't you try to disable soft key-pad like this

PINLockactivity.java

    //text field for input sequrity pin
    txtPin=(EditText) findViewById(R.id.txtpin);
    txtPin.setInputType(
      InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_VARIATION_PASSWORD);
    txtPin.setSelection(txtPin.getText().length());
    txtPin.setTextSize(22);
    txtPin.setSingleLine(true);


    //disable keypad
    txtPin.setOnTouchListener(new OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event) {

              int inType = txtPin.getInputType(); // backup the input type
              txtPin.setInputType(InputType.TYPE_NULL); // disable soft input
              txtPin.onTouchEvent(event); // call native handler
              txtPin.setInputType(inType); // restore input type
                return true; // consume touch even
        }
        });

and for this EditText Field

xml code is

<EditText android:layout_width="wrap_content" 
            android:id="@+id/txtpin"  
            android:maxLength="4" 
            android:layout_height="37dp" 
            android:gravity="center_horizontal" 
            android:longClickable="false" 
            android:padding="2dp"

            android:inputType="textPassword|number" 
            android:password="true" 
            android:background="@drawable/edittext_shadow" 
            android:layout_weight="0.98" 
            android:layout_marginLeft="15dp">
                <requestFocus></requestFocus>
   </EditText>

this is working fine with me for input security PIN with cursor.

i am taking input from button not keypad.

I have finally found a (for me) working solution to this.

First part (in onCreate):

// Set to TYPE_NULL on all Android API versions
mText.setInputType(InputType.TYPE_NULL);
// for later than GB only
if (android.os.Build.VERSION.SDK_INT >= 11) {
    // this fakes the TextView (which actually handles cursor drawing)
    // into drawing the cursor even though you've disabled soft input
    // with TYPE_NULL
    mText.setRawInputType(InputType.TYPE_CLASS_TEXT);
}

In addition, android:textIsSelectable needs to be set to true (or set in onCreate) and the EditText must not be focused on initialization. If your EditText is the first focusable View (which it was in my case), you can work around this by putting this just above it:

<LinearLayout
  android:layout_width="0px"
  android:layout_height="0px"
  android:focusable="true"
  android:focusableInTouchMode="true" >
    <requestFocus />
</LinearLayout>

You can see the results of this in the Grapher application, free and available in Google Play.

Note/edit: It is not necessary to derive from EditText to create your own when using this method to prevent the cursor from being disabled.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!