问题的EditText拦截空格键 [英] Problems intercepting space key in EditText

查看:246
本文介绍了问题的EditText拦截空格键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使一个用户输入文本从一个EditText TextView中的一种方式。但是,如果用户输入的东西,并希望解决它,我希望他们能够preSS空间空的EditText得到他们回信的最后一件事。第一个问题是,如果他们输入你好,按下回车键,将其添加到TextView的(它清除它从EditText上),然后打的空间,然后的EditText有你好。不是我想要的,我想不通为什么。

I'm attempting to make a way for a user to input text into a TextView from an EditText. However, if the user enters something, and wants to fix it, I want them to be able to press space on an empty EditText to get the last thing they wrote back. The first problem is, if they type in "hello", hit enter to add it to the TextView (which clears it from the EditText), then hit space, the EditText then has " hello". Not what I want, and I can't figure out why.

我的code放置输入的文本放入临时字符串:

My code to place the entered text into a holding string:

b1 = ti.getText().toString();

然后,如果用户点击空格键,我相信他们应该在的EditText得到B1。相反,我得到:+ B1 。这是为什么增加的空间在那里?

Then, if the user hits the space key, I believe they should get b1 in the EditText. Instead, I get: " " + b1. Why is this added space in there?

if((event.getAction()==KeyEvent.ACTION_DOWN)&&(key == KeyEvent.KEYCODE_SPACE)){

    if(ti.getText().toString().equals("")){
    ti.setText(b1);
}

我的第二个,更大的问题是,上述code只能在硬件键盘。对于软件键盘pressing空间的关键事件是什么?

My second, bigger problem is that the above code only works on a hardware keyboard. What is the key event for a software keyboard pressing space?

这是一切都在onKeyListener。

This is all in an onKeyListener.

推荐答案

这是简单的方法来解决了额外的空间问题是你把它放入之前的EditText只是从字符串中删除空格

An easy way to solve the extra space problem is to just remove the space from the String before you put it into the EditText

b1 = b1.substring(1); //<--- cut out the first character, which is the " " in your case.
ti.setText(b1);

P.S。我强烈建议更具描述性的变量名。你的程序有可能会造成混乱,如果你使用像TI和B1的名字上下工夫。也许这些选择使你的程序的上下文中更有意义。但是从你这里显示的是什么不容易分不清什么这些名称指。

p.s. I strongly recommend more descriptive variable names. Your programs are likely to be confusing to work on if you use names like ti and b1. Perhaps these choices make more sense in the context of your program. But from what you've shown here it is not easy to tell what these names refer to.

这篇关于问题的EditText拦截空格键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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