android EditText或AutoCompleteTextView setOnKey事件問題


今天 修改項目想把首頁搜索改下,當用戶輸入完關鍵字按下軟件鍵盤回車后跳轉結果頁

代碼沒問題不知道為什么老是跳轉二次,Debug調試發現setOnKeyListener執行二次一直未找到解決辦希望有遇到這問題的人指教下

最后查了下中文版的API發現有一個setOnEditorActionListener事件

我把搜索事件setOnKeyListener改為setOnEditorActionListener事件

然后把XML改為 

<EditText
android:id="@+id/searbox"
style="@style/indexsearchbox"
android:layout_toLeftOf="@id/searchbtn"
android:hint="@string/pleasekey" 
android:layout_height="45dip"
android:singleLine="true"
android:imeOptions="actionGo"/>這個意思是將軟件鍵盤上的回車鍵改為GO

  

代碼:

//單擊軟件鍵盤回車事件

//單擊軟件鍵盤回車事件
	    txtkeyword.setOnEditorActionListener(new EditText.OnEditorActionListener() { 
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				// TODO Auto-generated method stub
				if(actionId ==EditorInfo.IME_ACTION_GO){
					 //跳轉activity
					return true;
					}else{
						Toast.makeText(context, R.string.key_word, Toast.LENGTH_SHORT).show(); 

						return true;
					}
				}  
				return false;
			}
		});

  

在測試與調試沒問題成功。

就這一個跳轉二次問題折騰幾個小時 哎。。。。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM