我想在点击{ - 1}}中的下一行后自动输入“ - ”。我该怎么做?
答案 0 :(得分:0)
您可以TextWatcher执行此操作。
editText.addTextChangedListener( new TextWatcher(){
@Override
public void onTextChanged( CharSequence txt, int start, int before, int count ) {
if( -1 != txt.toString().indexOf("\n") ){
// new line available
// TODO: 5/1/2018 do your logic here
editText.setText(editText.getText().toString().replace("\n","-"+"\n"));
}
}
} );
答案 1 :(得分:0)
如果您要捕获KEYCODE_ENTER
事件并执行特定操作,则应使用OnEditorActionListener
这样的内容:
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
if(keyEvent != null && keyEvent.getAction() != KeyEvent.ACTION_DOWN){
return false;
} else if(actionId == EditorInfo.IME_ACTION_NEXT || (keyEvent != null && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER)){
String text = textView.getText().toString() + "-";
mEditText.setText(text);
mEditText.setSelection(text.length());
return true;
} else {
return false;
}
}
});
答案 2 :(得分:0)
尝试此代码...定义edittext文本最大长度然后达到然后转到下一行然后放 - 符号。
edittext.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if (charSequence.toString().trim().length()<=20){
}
else{
et.setText("\n -");
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});