gpt4 book ai didi

安卓 : KeyListener of EditText not working

转载 作者:太空宇宙 更新时间:2023-11-03 12:40:31 27 4
gpt4 key购买 nike

我正在尝试过滤 EditText 的输入字符以仅允许有效的浮点值。 (例如:“12,45”,只有一个逗号,...)。

为此,我使用 KeyListener (不要与 onKeyListener 或 textChangeListener 混淆。我想防止用户输入无效数据)。

在 Android 2.3.6 和 3.2 上,keyListener.filter() 和 keyListener.getAcceptedChars() 不会被调用,我可以引入任何字符(不限于 .getAcceptedChars())

我发现关于 keyListener 的文档很少。如果您有很好的示例或文档,我会非常感兴趣。

知道为什么这不起作用吗?

我做错了什么?

Activity 代码示例:

    @Override

protected void onCreate(Bundle savedInstanceState)

{

super.onCreate(...);

setContentView(R.layout.editTextTest_lyt);

...

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

mEditTextTest.setKeyListener(new NumberCommaDecimalKeyListenerTest());

...}

布局代码示例:

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 代码示例:

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;
}

}

最佳答案

查看 Android 文档,他们就 KeyListener 声明:

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

对于方法 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.

因此,因为您返回了一个内容类型,所以 KeyListener 被忽略了。看起来这是不可能的,或者您找到解决方法了吗?

也许您可以创建自定义 TextWatcher相反,然后使用 EditText.addTextChangedListener(TextWatcher textWatcher) 在 EditText 上设置它.?

关于安卓 : KeyListener of EditText not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9082750/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com