其實很簡單,把selectall放在show後面就行了
final EditText input = new EditText(this); input.setText("XXX"); new AlertDialog.Builder(this).XXXX.show(); input.selectAll() final EditText input = new EditText(this);input.setText("XXX");new AlertDialog.Builder(this).XXXX.show();input.selectAll()
再補充下,如果需要彈出輸入對話方塊,並且全選預設值,並且彈出IME,結合上面的selectall和下面的程式碼片段即可
final AlertDialog dialog = ...; 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); } } }); final AlertDialog dialog = ...;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); } }});