private EditText depthRawChannel;
if(depthRawChannel != null){
// depthRawChannel.clearFocus();
depthRawChannel.addTextChangedListener(mTextWatcher);
depthRawChannel.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId,
KeyEvent event) {
Log.d(TAG, "onEditorAction actionId="+actionId);
//一开始只判断是回车键而没有判断 ACTION_UP以至于每次按回车都导致触发2次
if ( (actionId == EditorInfo.IME_ACTION_DONE) ||(actionId == EditorInfo.IME_ACTION_NEXT)||
((event != null && event.isShiftPressed()==false) &&
(event.getKeyCode() == KeyEvent.KEYCODE_ENTER) &&
(event.getAction() == KeyEvent.ACTION_DOWN )) ){
//validateSN(); //自定义操作
//return true; //表示本次操作完成 不再往外传播本事件
Log.d(TAG, "onEditorAction1");
hideSoftKeyboard(depthRawChannel,getContext());
depthRawChannel.setCursorVisible(false);
bottom_view.setVisibility(View.VISIBLE);
return true;
}
return false;
}
});
}
///
private TextWatcher mTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Log.d(TAG, "beforeTextChanged");
bottom_view.setVisibility(View.INVISIBLE);
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String value = depthRawChannel.getText().toString();
if ("".equalsIgnoreCase(value)){
channelValue = 0;
}
if(value != null && !("".equalsIgnoreCase(value)) && (Integer.parseInt(value) <= 15) ){
channelValue = Integer.parseInt(value);
}
if(value != null && !("".equalsIgnoreCase(value)) && (Integer.parseInt(value) > 15)){
Toast.makeText(getContext(), ".....",
Toast.LENGTH_SHORT).show();
channelValue = 0;
}
Log.d(TAG, "onTextChanged channelVale: "+ channelValue);
Message msg = Message.obtain();
msg.what = CameraOpt.MSG_SET_CHANNEL_VALUE;
msg.arg1 = channelValue;
mCameraOpt[1].getHandler().sendMessage(msg);
}
@Override
public void afterTextChanged(Editable s) {
Log.d(TAG, "afterTextChanged");
}
};
/* 隐藏或显示软键盘*/
public static void hideSoftKeyboard(EditText editText, Context context) {
if (editText != null && context != null) {
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
}
}
public static void showSoftKeyboard(EditText editText, Context context) {
if (editText != null && context != null) {
InputMethodManager imm = (InputMethodManager) context
.getSystemService(Activity.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, 0);
}
}
相关code