2017-01-16 30 views
1

我想禁用 Enter鍵進入下一行使autocompletetextview就像一個搜索框,並從選擇正確的文本後,如單線列出光標將被禁用,鍵盤消失阻止用戶通過按軟鍵可轉到下一行autocompletetextview

問題1:雖然我已經這樣做了光標的TextView而回,也許它會在這裏工作過,但直到我按返回鍵移動鍵盤仍然存在禁用。

問題2:因爲我已經嘗試了maxLines =「1」,lines =「1」以及更多的事情,但它不能正常工作。

我想要的:用戶只能從列表中選擇一個項目。

代碼:

<AutoCompleteTextView 
      android:id="@+id/list_names" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"   
      android:minWidth="150dp" 
      android:ems="10" 
      /> 

JAVA:

public void getList(){ 
     try{ 
      while(scanner.hasNextLine()){ 
       list.add(scanner.next()); 
      } 
     }catch (Exception e){e.printStackTrace();} 
    } 
    public void populate(){ 
     adapter = new ArrayAdapter<String> 
       (this,android.R.layout.simple_list_item_1,list); 
     autolist.setAdapter(adapter); 
    } 

我也試圖阻止鍵盤操作部分的KeyEvent

@Override 
      public boolean onKey(View v, int keyCode, KeyEvent event) { 
           if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER || keyCode == 66 || event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)) { 

         return false; 
       } 
       return false; 
      } 

仍然沒有工作。 按之前進入關鍵 Before pressing enter

後按下回車鍵 After Pressing enter key

+0

,則返回true;如果onKey方法的條件 –

+0

@UsmanRana不工作它作爲單行,因爲我把這條線=「1」,但假設我輸入M然後按Enter鍵,所以它移動到下一行,但我沒有看到M當我按下回到刪除我看到M正在被刪除。你懂了 ?它從來沒有阻止輸入或完成鍵,而只有一行是可見的,因爲我已經使用了行=「1」,這就是爲什麼 – SameerKhan1406

回答

2

android:inputType="text"屬性添加到AutoCompleteTextView禁用enter按鈕轉到下一行。

<AutoCompleteTextView 
      android:id="@+id/list_names" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"   
      android:minWidth="150dp" 
      android:ems="10" 
      android:inputType="text" 
      /> 

更新: 隱藏鍵盤上按下Enter鍵/鍵盤完成

autolist.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
       if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) { 
        Log.e("TAG","Done pressed"); 
       } 
       return false; 
      } 
     }); 

隱藏鍵盤上選擇的建議

autolist.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
       View view = getCurrentFocus(); 
       if (view != null) { 
        InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); 

       } 
      } 

     }); 
+0

工作,但哪些鍵碼是必要的,所以我把那一個,並刪除其他 – SameerKhan1406

+0

好吧,讓行= 「1」正在做的工作,但假設我輸入一個詞,然後按Enter鍵,然後列表顯示自動完成空框,如「67街AMI,991-get_lop」像這樣,是的,我刪除了鍵盤事件阻止 – SameerKhan1406

+0

'android:inputType =「文本」將改變'輸入'按鈕到'下一個'不需要'線= 1' incase項目超過1行 –