我正在使用TextWatcher,这样当我开始在我的编辑文本中输入内容时,它显示所有资本,但我需要添加一个功能,当我按ENTER
时,然后在输入时应该是小写字母。我该怎么做?
这是textwatcher
scene.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
String s=arg0.toString();
if(!s.equals(s.toUpperCase()))
{
s=s.toUpperCase();
scene.setText(s);
scene.setSelection(scene.getText().length());
}
}
});
以下是处理ENTER
事件的代码scene.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
String s = scene.getText().toString();
s=s+'\n';
scene.setText(s);
scene.setSelection(scene.getText().length());
ib2(scene);
return true;
}
return false;
}
});
答案 0 :(得分:1)
我猜您需要 toLowerCase
String input = "Cricket!";
String upper = input.toUpperCase(); //stores "CRICKET!"
String lower = input.toLowerCase(); //stores "cricket!"
在你的onKey方法中你喜欢这个
String s = scene.getText().toString();
s=s.toLowerCase();
scene.setText(s);
像这样更新你的setOnKeyListener方法
scene.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
String s = scene.getText().toString();
s=s.toLowerCase();
scene.setText(s);
scene.setSelection(scene.getText().length());
ib2(scene);
return true;
}
return false;
}
});