2010-03-15 51 views
0

我想通過按下鍵盤上的'完成'按鈕來關閉編輯首選項對話框(如圖http://twitpic.com/18ttdp所示)。關閉鍵盤上的android首選項對話框ACTION_DONE按

目前,按'完成'只是解散鍵盤,但離開對話框。

在我的應用程序的其他部分我用類似下面的攔截「完成」按鍵,並在我的活動執行動作代碼:

text.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
     @Override 
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
      if (actionId == EditorInfo.IME_ACTION_DONE) { 
       //do stuff here 
       return true; 
      } 
      return false; 
     } 
    }); 

不過,我不確定如何做實現這一目標在我的偏好活動或佈局xml中也有同樣的效果。

+0

當您攔截「完成」按鈕時,您是否可以通過在對話框中點擊「確定」按鈕時發生的相同事件? – Feet 2010-03-16 00:22:55

+0

我該怎麼做? – Damian 2010-03-16 09:31:20

+0

這個鏈接可能會有所幫助http://stackoverflow.com/questions/11236264/change-action-of-done-button-on-virtual-keyboard-android/27035143#27035143 – Nepster 2014-11-20 08:46:11

回答

0

,而不是添加監聽器那裏,你應該做一些與此類似:

getDialog().setOnKeyListener(new OnKeyListener() { 

    @Override 
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { 
     dialog.dismiss(); 
     return true; 
    } 
}); 

當按下鍵此代碼將關閉該對話框。

+0

謝謝,這消除了對話框,但不保留值。如果我可以訪問對話框中的共享首選項(SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);),並在解除文本字段的值之前存儲它的值,則會保存新值。但是,當您再次單擊該首選項時,會顯示舊值(它以某種方式被緩存)。我必須退出首選項屏幕,然後選擇首選項 - > pref_option in以顯示新保存的值。我如何修復這部分? – Damian 2010-03-16 09:37:24

+0

我應該警告你我不會這樣做。我覺得這會讓你的用戶界面不直觀。如果你想這樣做,嘗試添加像persistString(mValue);到那個代碼。 – Macarse 2010-03-16 11:18:21

+0

我用prefs.edit.putString()(persistString是一個受保護的方法)。正如我所說,它確實存儲了該值,但如果在解除對話框(從鍵盤按下)後立即再次選擇該首選項,則會顯示舊的(緩存的)值。 是否可以在顯示對話框時以某種方式刷新值? – Damian 2010-03-16 11:35:00

0

我有同樣的問題,這是我如何解決它:

// edit text to get input 
    final EditText input = new EditText(this); 
    //input.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL); 
    input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_IME_MULTI_LINE); 
    alert.setView(input); 

    // ok button 
    alert.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int which) { 
      // do stuff 
     } 
    }); 

對於我的需求,輸入了一個數字(因此註釋掉行),但如果你想要的文字使用那裏的東西。

0

這是我如何解決它:

final EditTextPreference namePref = (EditTextPreference) findPreference("name"); 
    namePref.getEditText().setOnEditorActionListener(new TextView.OnEditorActionListener() { 
     @Override 
     public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
      if(actionId == EditorInfo.IME_ACTION_DONE) { 
       namePref.onClick(null, DialogInterface.BUTTON_POSITIVE); 
       namePref.getDialog().dismiss(); 
       return true; 
      } 
      return false; 
     } 
    }); 

但你可能要考慮繼承EditTextPreference代替,因爲這的onClick調用是一個黑客,它的實現可能在未來改變。