Show soft keyboard (numeric) using AlertDialog.Builder
I have an EditText in an AlertDialog, but when it pops up, I have to click on the text box before the keyboard pops up. This EditText is declared in the XML layout as "number", so when the EditText is clicked, a numeric keypad pops up. I want to eliminate this extra tap and have the numeric keypad pop up when the AlertDialog is loaded.
All of the other solutions I found involve using
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
This is NOT an acceptable solution, as this results in the standard keyboard rather than the numeric keyboard popping up. Does anyone have a way to make the numeric keyboard pop up in an AlertDialog, preferably while keeping my layout defined in XML?
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Mark Runs")
.setView(markRunsView)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText runs = (EditText)markRunsView.findViewById(R.id.runs_marked);
int numRuns = Integer.parseInt(runs.getText().toString());
// ...
})
.setNegativeButton("Cancel", null)
.show();
Edit: I want to be perfectly clear that my layout already has:
android:inputType="number"
android:numeric="integer"
I also tried this:
//...
.setNegativeButton("Cancel", null)
.create();
EditText runs = (EditText)markRunsView.findViewById(R.id.runs_marked);
runs.setInputType(InputType.TYPE_CLASS_NUMBER);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();
but that also did not work. With the setSoftInputMode line, I still get the full keyboard when the AlertDialog loads; without it, I still get nothing. In either case, tapping on the text box will bring up the numeric keypad.
Edit Again:
This is the XML for the EditText
<EditText开发者_开发问答
android:id="@+id/runs_marked"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4dip"
android:inputType="number"
android:numeric="integer">
<requestFocus/>
</EditText>
Coming a bit late, but today I had this same problem. This is how I solved it:
Call the keyboard when the Dialog opens just like you did:
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Now, into the Dialog I'm assuming you have a EditText field that accepts only numbers. Just request focus on that field and the standard keybord will automatically transform into the numeric keyboard:
final EditText valueView = (EditText) dialogView.findViewById(R.id.editText);
valueView.requestFocus();
Now you just have to remember to dismiss the keyboard once you're done with the Dialog. Just put this in the positive/negative/neutral button click listener:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(valueView.getWindowToken(), 0);
Just try to set the InputType by using setInputType().
EditText runs = (EditText)markRunsView.findViewById(R.id.runs_marked);
runs.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
I think that you should keep on using the setSoftInputMethod hack but also provide hints to android that you want a numeric input.
Using xml layout attributes
For this, you can use several xml attributes to your EditText xml definition (see android:inputType for available options)
Examples:
<EditText android:inputType="phone" ...
<EditText android:inputType="number" ...
<EditText android:inputType="numberSigned" ...
<EditText android:inputType="numberDecimal" ...
You can also both hint android to show digital keyboard and restrict input to acceptable characters with android:numeric
Examples:
<EditText android:numeric="integer" ...
<EditText android:numeric="signed" ...
<EditText android:numeric="decimal" ...
Programatically
Use EditText.setRawInputType(int) with constants such as TYPE_CLASS_NUMBER you will find in android:inputType
or TextView.setKeyListener(new NumberKeyListener())
EDIT
AlertDialog focus by default on the positive button. It seems that it is what is causing trouble here. You may look at this similar question and its answer.
Try to specify in layout xml for your edit box:
<EditText ...>
<requestFocus/>
</EditText>
精彩评论