2010-06-25 84 views
0

我想在我的android應用程序內執行事件鼠標按下。當用戶在文本字段中輸入文本時輸入CARRIAGE RETURN時。當我在OnClickListener中爲該EditText UI檢測到該字符時,我想在ADD按鈕上按下鼠標按鈕。Android模擬按鈕按

回答

0

彼得 -

這聽起來像你想要做的是覆蓋在給定的EditText的EditorAction,然後以編程方式執行相同的動作OnClickListener。例如:

EditText inputText; //This is either created in code or inflated via XML 
Button addButton; //This is either created in code or inflated via XML 

inputText.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
     addButton.performClick(); 
     //Tell the system you consumed the action event 
     return true; 
    } 
}); 

的actionId可以成爲一個有用的屬性也一樣,它根據報告上顯示的軟鍵盤方法的具體行動(DONE,NEXT等)......但請記住,如果用戶按下從硬件鍵盤輸入的動作總是爲EditorInfo.IME_NULL,因此它可能無法用於監視此值。

與重寫KeyEvent偵聽器相比,這是一種更安全的方法,因爲您消耗的事件的風險較低,您不需要並且不知道您偷了。

希望有幫助!