EditText把軟鍵盤上的回車鍵改爲搜索、發送或者 下一步,窗口隨軟鍵盤彈出而改變。

Android Edittext獲取焦點後,彈出的軟鍵盤顯示搜索、發送、完成等功能

在EditText加一個屬性

android:imeOptions="actionSearch"(搜索)

android:imeOptions="actionSend"(發送)   

android:imeOptions="actionNext"(下一項),

android:imeOptions="actionDone"(完成),

android:imeOptions="actionGo"(前往)等等;

有時候

android:imeOptions="actionSearch"(搜索)會不起作用,會顯示換行

這是因爲在2.3及以上版本不起作用,2.3以下就好使

這時加上android:singleLine="true"即可

如果設置了輸入類型

如:android:inputType="number"

android:imeOptions="actionSearch"(搜索)也會不起作用,會顯示換行

 

 

有時軟鍵盤會把原來的佈局擠上去

可以在androidMainfest.xml文件中在此Activity中寫入 android:windowSoftInputMode="adjustPan"

 

或者當返回時  先去隱藏軟鍵盤

InputMethodManager inputmanger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                inputmanger.hideSoftInputFromWindow(arg0.getWindowToken(), 0);

有時候爲了佈局美觀,在搜索時沒有搜索按鈕,而是調用軟件盤上的按鈕。調用的實現只需要在XML在輸入框中加入Android:imeOptions="actionSearch",另外,還要設置android:singleLine="true",保證點擊不會換行,最後調用軟鍵盤時,回車鍵就會顯示搜索二字。

然後調用 OnEditorActionListener,不是OnKeyListener

?

在CODE上查看代碼片
  1. et_search.setOnEditorActionListener(new TextView.OnEditorActionListener() {  
  2.     @Override  
  3.     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {  
  4.         if (actionId == EditorInfo.IME_ACTION_SEARCH){  
  5.             isSearch = true;  
  6.             page = 1;  
  7.             MyUtils.hideSoftKeyboard(EnterShopActivity.this,v);  
  8.             getData();  
  9.             return true;  
  10.         }  
  11.         return false;  
  12.     }  
  13. });  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章