2012-03-21 158 views
7

我在Android中使用AlertDialog.Builder快速提示用戶輸入文字。該對話框出現並且效果很好,但用戶必須點擊EditText字段才能加載軟鍵盤。有沒有辦法打開鍵盤,並在打開對話框時關注焦點?這裏是我的代碼:默認焦點和鍵盤到Android AlertDialog中的EditText

final Map<String,Object> rowData = itemList.get(mPosition); 
        final EditText input = new EditText(searchList.getContext()); 
       input.requestFocus(); 


       input.setSingleLine(); 
       final AlertDialog dialog = new AlertDialog.Builder(searchList.getContext()) 
       .setTitle(StringUtils.getSafeString(rowData.get("label"))) 
       .setView(input) 
       .setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         rowData.put("value", StringUtils.getSafeString(input.getText())); 
         searchList.invalidateViews(); 

        } 
       }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         // Do nothing. 
        } 
       }).create(); 
       dialog.show(); 

回答

12

使用下面的代碼中。它爲我工作。

editText.setOnFocusChangeListener(new OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 
      editText.post(new Runnable() { 
       @Override 
       public void run() { 
        InputMethodManager inputMethodManager= (InputMethodManager) YourActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE); 
        inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT); 
       } 
      }); 
     } 
    }); 
    editText.requestFocus(); 
+0

非常感謝,完美的作品! – 2017-10-30 23:52:22

-2
在你的XML佈局

呼叫

<requestFocus/> 

默認的EditText

<EditText 
android:blabla 
.... 
<requestFocus/> 
/> 
+1

的AlertDialog.Builder不使用XML的佈局,只需要在在代碼中創建一個EditText。我已經嘗試過在多個地方調用生成的EditText的請求焦點,而且它看起來並不像我希望的那樣工作。這不是這個問題的工作解決方案。 – cain 2012-03-22 12:21:00

0

InputMethodManager IMM =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); 隱藏鍵盤使用:

InputMethodManager imm =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 012.htmlSoftInputFromWindow(view.getWindowToken(),0);

,或者嘗試下面的代碼,但你必須在requestFocus()方法或設置爲您的EditText

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     if (hasFocus) { 
      dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
     } 
    } 
}); 
5

隱藏鍵盤時,編程設置側重於一個EditText在Android對話框。

我也有這個問題,這是一個非常簡單的修復 - 這是我建議的解決方案。雖然它爲我工作在DialogFragments上,但我沒有看到爲什麼它不適用於您的情況。

基本上沒有觸發軟鍵盤,因爲視圖是以編程方式創建的。實際的修復程序簡單地把該線路onCreateDialog方法:

dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); 

從Android文檔上DialogFragments

如果用戶集中在一個EditText,軟鍵盤將 自動出現。爲了迫使我們的 編程焦點發生,我們將其稱爲 getDialog()。getWindow()。setSoftInputMode()。請注意,許多先前在對話框中完成的Window 操作仍然可以在DialogFragment中完成 ,但您必須調用getDialog()。getWindow() 而不是僅調用getWindow()。

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    //setup your dialog builder and inflate the view you want here 
    ... 
    //Make sure your EditText has the focus when the dialog is displayed 
    edit.requestFocus(); 
    //Create the dialog and save to a variable, so we can set the keyboard state 
    Dialog dialog = builder.create(); 
    //now, set to show the keyboard automatically 
    dialog.getWindow().setSoftInputMode(LayoutParams.SOFT_INPUT_STATE_VISIBLE); 
    return dialog; 
} 
+1

要走的路!這應該是被接受的答案。我使用了'getWindow()。setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);'在我的AppCompatDialog子類的構造函數中。 – 2015-12-15 18:45:22

+0

dialog.getWindow()可能返回null – 2017-06-03 01:39:26

0

使用自定義視圖如果你需要它往往

public class RequestFocusEditText extends AppCompatEditText { 
    private RequestFocusEditText self; 

    public RequestFocusEditText(final Context context, AttributeSet attrs) { 
     super(context, attrs); 
     this.self = this; 

     setOnFocusChangeListener(new OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       post(new Runnable() { 
        @Override 
        public void run() { 
        InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
        inputMethodManager.showSoftInput(self, InputMethodManager.SHOW_IMPLICIT); 
       } 
      }); 
     } 
    }); 
    requestFocus(); 
    } 
}