et.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean hasFocus) { if(hasFocus){ //有焦點 }else { //無焦點 } } });
et.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View view, int keycode, KeyEvent keyEvent) { if (keycode == KeyEvent.KEYCODE_ENTER) { //回車事件 } return false; //返回false會繼續執行回車換行,返回true不會執行回車換行 } }); et.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) { // actionUnspecified 未指定,對應常量EditorInfo.IME_ACTION_UNSPECIFIED.效果: // actionNone 沒有動做,對應常量EditorInfo.IME_ACTION_NONE 效果: // actionGo 去往,對應常量EditorInfo.IME_ACTION_GO 效果: // actionSearch 搜索,對應常量EditorInfo.IME_ACTION_SEARCH 效果: // actionSend 發送,對應常量EditorInfo.IME_ACTION_SEND 效果: // actionNext 下一個,對應常量EditorInfo.IME_ACTION_NEXT 效果: // actionDone 完成,對應常量EditorInfo.IME_ACTION_DONE 效果: return false; } });
et.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) { // 輸入內容的時候 } @Override public void afterTextChanged(Editable editable) { // 輸入內容以後 } });
*在佈局文件中設置html
<EditText android:id="@+id/variableValue" ...... android:inputType="number" />
*在代碼中設置java
et.setInputType(inputType);
相關的參數:android
相關參數
ide
android:focusable="true" android:focusableInTouchMode="true"