Android Custom EditText not showing cursor in ICS
I have an EditText in my application which is to only receive inputs from buttons I have placed on the screen.
To avoid the soft keyboard appearing I have a customised EditText class as follows:
public class CustomEditText extends EditText {
public CustomEditText(Context context) {
super(context);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
// Disables Keyboard;
public boolean onCheckIsTextEditor() {
return false;
}
}
This successfully stops the keyboard from appearing, however in ICS this approach also stops the Cursor from appearing.
setCursorVisible(true)
does not have any effect.
I've tried alternate methods of keeping the soft keyboard hidden, such as using android:editable="false"
and .setKeyListener(null);
but none of these solutions have ever worked in my tests. The keyboard always appears.
So, is there a way to return the cursor in ICS, while keeping the onCheckIsTextEditor override as it is?
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.
链接地址: http://www.djcxy.com/p/58652.html