一、设置EditText只显示一行
android:maxLines=“1”
android:singleLine=“true”
android:inputType=“text”
二、监听输入
private TextWatcher BTNameOutSizeListener = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
xxx添加处理
};
初始化时:
mBtNameEdit.setFilters(new InputFilter[]{new InputFilter.LengthFilter(MAXLEN)});
mBtNameEdit.setText(mLocalManager.getBluetoothAdapter().getName());
mBtNameEdit.addTextChangedListener(BTNameOutSizeListener);
尽量在setText之后在设置listener
**设置EditText光标定位在最后,必须放在setText之后,不然没有效果:
mBtNameEdit.setText(mLocalManager.getBluetoothAdapter().getName());
mBtNameEdit.setSelection(mBtNameEdit.getText().length());
三、点击EditText后的软键盘操作
当编辑完内容后,点击软键盘上的按键时,监听点击事件后进行一系列处理的接口:setOnEditorActionListener
mBtNameEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE
|| (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
}
return false; //返回true,保留软键盘。false,隐藏软键盘
}
});