2012-07-30 19 views
3

我有一個AlertDialog我在其中設置XML作爲其視圖。在那xml layout我有一個EditText。但在EditText中輸入數據後,如果我嘗試使用退格刪除,則字符不會被刪除(它的退格不起作用)。Android Editext退格鍵有問題

我錯過了什麼嗎?我搜索,但沒有得到任何適當的解決方案,除了添加keylistener。我認爲它應該工作簡單?

任何人都可以幫助我。

這裏是我的EditText

<EditText 
     android:id="@+id/TextBox" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:inputType="text"> 
     <requestFocus /> 
</EditText> 

對話框ceation代碼:

hintDialog = new AlertDialog.Builder(activity) 
    .setTitle("Enter Your Hint:") 
    .setView(hintDialogView).create(); 
    hintDialog.setOnKeyListener(new DialogInterface.OnKeyListener() { 
     @Override 
     public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { 
     if(keyCode == KeyEvent.KEYCODE_BACK) 
     hintDialog.dismiss(); 
     return true; 
     } 
    }); 
+0

請粘貼'AlertDialog'創建部分 – sunil 2012-07-30 07:03:51

+0

@sunil的代碼,請參閱我的更新問題。 – 2012-07-30 07:07:42

回答

8

你有任何onKeyListeners集?這可能是問題的原因。

試試這個:

hintDialog = new AlertDialog.Builder(activity) 
.setTitle("Enter Your Hint:") 
.setView(hintDialogView).create(); 
hintDialog.setOnKeyListener(new DialogInterface.OnKeyListener() { 
    @Override 
    public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { 
    if(keyCode == KeyEvent.KEYCODE_BACK) 
    hintDialog.dismiss(); 
    return true; 
    } 
    return false; 
}); 

(添加返回FALSE;)

+0

你是我的節省時間...超過3小時尋找這個確切的修復.. – 2017-07-15 10:04:12

1

你有KeyEvent.KEYCODE_BACK監聽? 調用來處理關鍵事件。您可以覆蓋它以在將所有關鍵事件分派到窗口之前攔截它們。確保爲應該正常處理的關鍵事件調用此實現。 當你重載dispatchKeyEvent方法時,你必須在返回時調用super.dispatchKeyEvent(event)。

0

不應該KEYCODE_BACK但KEYCODE_DEL代替。它適用於你的情況。