Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

android - Enforcing Input Method of Edit Text

I've tried searching around for an answer to this but I can't seem to find an answer.

Is it possible for me to prevent a user from changing the input method of an edittext that I have created? Currently I have an edittext which I would like to only accept numbers from the user by displaying a soft keyboard similiar to the phone keypad input. By declaring the following in XML :

    <EditText 
    android:layout_height="wrap_content"    
    android:layout_width="fill_parent"    
    android:singleLine="true"     
    android:inputType="phone"
    android:numeric="decimal"       
    />

And also attaching a custom DigitsKeyListener to the edittext that returns InputType.TYPE_CLASS_PHONE in the getInputType method, I have succesfully made the edittext only accept decimal numbers as well as use the soft keyboard that I want.

The problem however starts if the user is not using the default Android Keyboard. If the user has changed their input method to a custom keyboard or to certain locales such as the Japanese IME, the soft keyboard that is shown becomes lacking for my application's purpose.

This can be achieved by long holding on the edittext and selecting a different input method, or changing it in another application will also affect my application.

My question is, is there any possible way for me to prevent other Input Methods from being used in my application? I want my application to always use the default Android Keyboard, and nothing else.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As far as I know, it's not possible to force a user to use a specific system keyboard inside your application (reference: android app specific soft keyboard). That reference does show you how to create your own, though. It wouldn't be terribly difficult to create a View that pops up when the EditText is focused that provides the user with a set of numbers.

You can also set the android:digits="0123456789" attribute in your EditText to disallow other characters from being entered.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...