简体   繁体   中英

EditText - keyboard doesn't appear on focus

I have this annoying problem. My app has 2 activities (tabs) Activity1:listview, Activity2:editText+listview. App starts with Tab1(Activity1). When i open 2nd Activity (with edittext), no matter if EditText is selected or not (programmable), when i click on EditText, nothing happens (softKeyboard should appear). Only solution is to change activity (click on Tab1 widget) and return to activity 2 - after this tab swap, keyboard works fine.

Part of XML layout with edittext:

    <EditText
    android:hint="Wyszukaj..."
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:inputType="textAutoComplete|text"
    android:singleLine="true" 
    android:focusable="true"
    android:focusableInTouchMode="true"
>

and here is 2 overrided method from Activity2

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tab2);

    this.db = DataBase.getInstance();
    this.ds = DataSource.getInstance();
    this.prepareListView();
}

@Override
protected void onResume() {
    super.onResume();
    this.doubleBackToExitPressedOnce = false;
}
private void prepareListView() {
    sbal = this.db.getAllStops();
    adapter = new StopListAdapter(this, sbal);

    lv = (ListView) findViewById(R.id.tab2list);
    lv.setAdapter(adapter);
    lv.setOnItemClickListener(onClick);
    EditText et = (EditText) findViewById(R.id.editText1);
    et.addTextChangedListener(changeWatcher);
    registerForContextMenu(lv);
}

Do you have any ideas, how XMLcode and activity code should look like in this case?

See this answer. It solved the same problem for me:

Keyboard not shown when i click on edittextview in android?

and try this code

mEditText.clearFocus();

The workaround for this, to explicitly call the soft keyboard on onCreate() and onResume() method.

 editText.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            if (editText.isEnabled() && editText.isFocusable()) {
                editText.post(new Runnable() {
                    @Override
                    public void run() {
                        Context context = getApplicationContext();
                        final InputMethodManager imm = 
          (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(editor,InputMethodManager.SHOW_IMPLICIT);
                    }
                });
            }
        }
    });

Hope this helps :)

try dropping out the two lines about "focusable" from xml. i have something very similar and it works without them

try {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(
                            YourEditText.getWindowToken(), 0);
                } catch (Exception e) {
                    e.printStackTrace();
                }

Try this with edittext...

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