1

我正在尝试获取空格、输入、清除按钮的值,但没有成功我也尝试显示敬酒消息,当我按下回车、空格、清除(其中一个)时,它应该给我一条消息“按下 Enter” . 它应该只发生在虚拟键盘上。我只得到字符值。像“按下:A”

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    et = (EditText)findViewById(R.id.entry);
    et.addTextChangedListener(this);


}
@Override
public void afterTextChanged(Editable s) 
{
    Toast.makeText(this,"Pressed : " +s.toString() ,Toast.LENGTH_LONG).show();

}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after)
{

}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{

}
4

1 回答 1

1

将您的 afterTextChanged(Editable s) 修改为:

@Override
public void afterTextChanged(Editable s) 
{
    String str = s.toString();
    if(str.substring(str.length()-1, str.length()).equals(" "))
            Toast.makeText(this,"Pressed : Space",Toast.LENGTH_SHORT).show();
    else if(str.substring(str.length()-1, str.length()).equals("\n"))
        Toast.makeText(this,"Pressed : Enter",Toast.LENGTH_SHORT).show();
    else
            Toast.makeText(this,"Pressed : " +str.substring(str.length()-1, str.length()),Toast.LENGTH_SHORT).show();
}

它会解决你的问题。

于 2012-07-02T11:04:29.757 回答