问题
In my Android application, I want an EditText with android:editable="false" but the cursor blinking. The cursor blinking seems doesn't work after "editable" is set to false.
I just want to use my own Keyboard widget(not the system's soft keyboard), and keep the cursor blinking.
Is there any idea to make that possible?
回答1:
Maybe try leaving out the xml attribute android:editable entirely and then try the following in combination to
keep the cursor blinking and prevent touch events from popping up a native IME(keyboard)..
/*customized edittext class
* for being typed in by private-to-your-app custom keyboard.
* borrowed from poster at http://stackoverflow.com/questions/4131448/android-how-to-turn-off-ime-for-an-edittext
*/
public class EditTextEx extends EditText {
public EditTextEx(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onCheckIsTextEditor() {
return false; //for some reason False leads to cursor never blinking or being visible even if setCursorVisible(true) was called in code.
}
}
Step 2
change the above method to say return true;
Step 3 Add another method to above class.
@Override
public boolean isTextSelectable(){
return true;
}
Step 4
In the other location where the instance of this class has been instantiated and called viewB I added a new touch event handler
viewB.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
viewB.setCursorVisible(true);
return false;
}
});
Step 5 Check to make sure XML and or EditText instantiation code declares IME/keyboard type to be 'none'. I didnt confirm relevance, but Im also using the focusable attributes below.
<questionably.maybe.too.longofa.packagename.EditTextEx
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true"
android:inputType="none">
Sorry for so many xml attributes. My code uses them all, testing in 4.2.1, and has results.
Hope this helps.
回答2:
You can use either the xml attribute
android:cursorVisible="false"
or the java function
setCursorVisible(false).
it will work
回答3:
Just Adding this method for anyone looking for and answer. I have tried many methods but only this one worked from me.
public static void disableSoftKeyboard(final EditText v) {
if (Build.VERSION.SDK_INT >= 11) {
v.setRawInputType(InputType.TYPE_CLASS_TEXT);
v.setTextIsSelectable(true);
} else {
v.setRawInputType(InputType.TYPE_NULL);
v.setFocusable(true);
}
}
回答4:
I called the following from onCreate(), but this affects all EditTexts.
private void hideKeyboard ()
{
getWindow ().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
getWindow ().setFlags (WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
}
来源:https://stackoverflow.com/questions/12870577/disable-input-method-of-edittext-but-keep-cursor-blinking