安卓的EditText的KeyListener的不工作 [英] Android : KeyListener of EditText not working

查看:129
本文介绍了安卓的EditText的KeyListener的不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想筛选一个EditText的输入字符,只允许有效的浮点值。 (例如:12,45,只有一个逗号,...)。

对于这一点,我使用的KeyListener (不要混淆与onKeyListener或textChangeListener,我想的 prevent 的进入无效数据的用户)。

在Android 2.3.6和3.2 keyListener.filter()和keyListener.getAcceptedChars()不叫,我可以介绍任何字符(不限于.getAcceptedChars())

我发现有关的KeyListener小单证。如果你有一个很好的例子或医生,我很感兴趣。

任何线索,为什么这是不工作?

我做了什么错了?

活动code样品:<​​/ P>

  @覆盖    保护无效的onCreate(捆绑savedInstanceState)    {        super.onCreate(...);        的setContentView(R.layout.editTextTest_lyt);... mEditTextTest =(EditText上)findViewById(R.id.editTextTest);mEditTextTest.setKeyListener(新NumberCommaDecimalKeyListenerTest());...}

布局code样品:<​​/ P>

  editTextTest_lyt.xml:
            &LT;的EditText
                机器人:ID =@ + ID / editTextTest
                机器人:layout_height =FILL_PARENT
                机器人:layout_width =FILL_PARENT
                机器人:layout_weight =1
                安卓的inputType =numberSigned | numberDecimal
                机器人:EMS =5
                机器人:最大长度=9
                机器人:MAXLINES =1
                机器人:重力=右| center_vertical
                机器人:imeOptions =actionNext/&GT;

的KeyListener code样品:<​​/ P>

 公共类NumberCommaDecimalKeyListenerTest扩展NumberKeyListener {私有静态最终炭[] mAccepted =新的char [] {'0','1','2','3','4','5','6','7','8',' 9',' - ',','};
    私有静态最终布尔mSign = TRUE;
    私有静态最终布尔mDecimal = TRUE;    @覆盖    受保护的char [] getAcceptedChars()
    {
      Log.e(LODA,KeylistenerTest.getAcceptedChars()); // DONT日志中显示出来
        返回mAccepted;
    }    公共NumberCommaDecimalKeyListenerTest()
    {超();
  Log.e(LODA,CREATE KeylistenerTest); //在日志中显示出来
    }    @覆盖
    公众诠释getInputType()
    {
    Log.e(LODA,KeylistenerTest.getInputType()); //在日志中显示出来
        INT的contentType = InputType.TYPE_CLASS_NUMBER;
        如果(mSign)
        {
            的contentType | = InputType.TYPE_NUMBER_FLAG_SIGNED;
        }
        如果(mDecimal)
        {
            的contentType | = InputType.TYPE_NUMBER_FLAG_DECIMAL;
        }
        返回的contentType;
    }    @覆盖
    公共CharSequence的过滤器(CharSequence的来源,诠释开始,诠释年底,跨区DEST,诠释DSTART,诠释DEND)
    {
    Log.e(LODA,输入KeylistenerTest.filter); // DONT日志中显示出来        //我的逻辑会在这里?        //如果用户pressed'。',改变了,
        来源= source.toString()代替(,);
        。来源= source.toString()代替(6,9);        CharSequence的OUT = super.filter(源,开始,结束,DEST,DSTART,DEND);        返回的;
    }}


解决方案

看着如其陈述的Andr​​oid文档到的KeyListener 是:


  

请注意,对于大多数情况下,这个接口已经被普遍所取代
  由INPUTMETHOD定义软输入法;它应该只被使用
  对于情况下,一个应用程序有其自己的屏幕上的键盘和也
  要处理硬键盘事件,与之相匹配的


和该方法 getInputType()


  

如果您返回TYPE_NULL则没有软键盘会提供。其他
  也就是说,你必须提供自己的键盘上屏输入和
  关键监听器将被用于从硬键盘处理输入。


  
  

如果您返回任何其他值,软输入法将被创建
  当用户将焦点在编辑器中,这将提供一个小键盘
  同时也消耗硬键事件。 这意味着,关键听众
  通常将不被使用,而不是软输入方法将
  护理管理键输入按这里返回的内容类型。


所以,因为你返回的内容键入的KeyListener被忽略。看起来这是不可能的,那么还是你发现身边工作?

也许你可以创建一个自定义 TextWatcher 代替,然后使用这个设置上的EditText <一个href=\"http://developer.android.com/reference/android/widget/TextView.html#addTextChangedListener%28android.text.TextWatcher%29\">EditText.addTextChangedListener(TextWatcher textWatcher)?

I'm trying to filter the input char of an EditText to allow only valid float value. (e.g. :"12,45", only one comma, ...).

For this, I use the KeyListener (dont confuse with onKeyListener or textChangeListener. I want to prevent the user from entering invalid data).

On Android 2.3.6 and 3.2 the keyListener.filter() and keyListener.getAcceptedChars() are not called and I can introduce any char (not limited to .getAcceptedChars())

I found little documentations about keyListener. If you got a good example or doc, I'm very interested.

Any clue why this is not working ?

What did I do wrong ?

Activity Code sample:

    @Override

    protected void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(...);

        setContentView(R.layout.editTextTest_lyt);

...

 mEditTextTest = (EditText) findViewById(R.id.editTextTest);

mEditTextTest.setKeyListener(new NumberCommaDecimalKeyListenerTest());

...}

Layout Code sample:

editTextTest_lyt.xml:
            <EditText
                android:id="@+id/editTextTest"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent"
                android:layout_weight="1"
                android:inputType="numberSigned|numberDecimal"
                android:ems="5"
                android:maxLength="9"
                android:maxLines="1"
                android:gravity="right|center_vertical"
                android:imeOptions="actionNext"/>

KeyListener Code sample:

public class NumberCommaDecimalKeyListenerTest extends NumberKeyListener {

private static final char[] mAccepted = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', ',' };
    private static final boolean    mSign = true;
    private static final boolean    mDecimal = true;

    @Override

    protected char[] getAcceptedChars()
    {
      Log.e("LODA", "KeylistenerTest.getAcceptedChars()"); // DONT show up in logs 
        return mAccepted;
    }

    public NumberCommaDecimalKeyListenerTest()
    { super();
  Log.e("LODA", "CREATE KeylistenerTest"); // show up in logs 
    }

    @Override
    public int getInputType()
    {
    Log.e("LODA", "KeylistenerTest.getInputType()"); // show up in logs 
        int contentType = InputType.TYPE_CLASS_NUMBER;
        if (mSign)
        {
            contentType |= InputType.TYPE_NUMBER_FLAG_SIGNED;
        }
        if (mDecimal)
        {
            contentType |= InputType.TYPE_NUMBER_FLAG_DECIMAL;
        }
        return contentType;
    }

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
    {
    Log.e("LODA", "enter KeylistenerTest.filter"); // DONT show up in logs 

        // My logic will go here...

        // if user pressed '.', is changed for ','
        source = source.toString().replace(".", ",");
        source = source.toString().replace("6", "9");

        CharSequence out = super.filter(source, start, end, dest, dstart, dend);

        return out;
    }

}

解决方案

Looking at the Android docs they state in relation to KeyListener that:

Note that for most cases this interface has been superceded by general soft input methods as defined by InputMethod; it should only be used for cases where an application has its own on-screen keypad and also wants to process hard keyboard events to match it

And for the method getInputType():

If you return TYPE_NULL then no soft keyboard will provided. In other words, you must be providing your own key pad for on-screen input and the key listener will be used to handle input from a hard keyboard.

If you return any other value, a soft input method will be created when the user puts focus in the editor, which will provide a keypad and also consume hard key events. This means that the key listener will generally not be used, instead the soft input method will take care of managing key input as per the content type returned here.

So because you return a content type the KeyListener is ignored. Looks like this isn't possible then or did you find a work around?

Perhaps you could create a custom TextWatcher instead and then set this on the EditText using EditText.addTextChangedListener(TextWatcher textWatcher).?

这篇关于安卓的EditText的KeyListener的不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆