简体   繁体   中英

Android EditText not showing keyboard

I have just started programming on Android and have completed the MyFirstApp tutorial:

http://developer.android.com/training/basics/firstapp/building-ui.html

The app runs, however when I touch the text field, the soft keyboard does not popup.

<EditText
    android:id="@+id/edit_message"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="@string/edit_message"
    android:focusable="true"
    android:focusableInTouchMode="true" 
    >
</EditText>

I have tried adding:

  <requestFocus />

but that makes no difference.

I have added this to the mainifest, but that also makes no difference:

android:windowSoftInputMode="stateAlwaysVisible"

Surely this kind of functionality is already built in and I don't have to create a custom function (method) to achieve this ? - I would have expected the tutorial to at least mention that the keyboard won't popup using the provided code, as I have spent the last 3 hours trying to figure out what was wrong with my code !

EDIT

I am working on the latest 4.42 and have added the clearFocus() however that makes no difference:

public void sendMessage(View view) {
    Intent intent = new Intent(this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.edit_message);
    
    editText.clearFocus();
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);

} 

It would help if you said which version you was working on (2.3/4.4 etc) but assuming it is 2.1-2.3 then this link may help you:

https://stackoverflow.com/a/7743066/2978914

It states that you should clear the focus of the Edit text itself.

Direct quote from link above:

OK, This might be a late response, but it worked.

I have met this problem on android 2.1 and 2.3.x(not tested on other versions of SDKs).

I noticed a strange thing that when my click on the EditText was unable to open the keyboard, I pressed the BACK button to show an alert dialog and then I canceled(closed) it, and clicked the EditText again, now the keyboard was brought to life again.

From that I can conclude that the keyboard will always show for the EditText if the EditText does not previously own focus(showing an alert dialog over the EditText view will make the EditText to lose focus).

so call the function below on your EditText when it is brought to front:

mEditText.clearFocus(); or

parentViewThatContainsEditTextView.clearFocus();

OK, this appears to be a bug in version 18 (4.4), when I compile for version 14, the keyboard appears.

This works:

android:targetSdkVersion="14" />

This does not:

android:targetSdkVersion="18" />

For anyone who's still struggling with the keyboard which is not opening when clicking on EditText please check to have android:inputType set.

Without it, the keyboard won't open.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM