Cannot resolve symbol showsoftinput

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 17:14:56

问题


I'm trying to force the numeric keypad to show when my activity and EditText load. It seems there's a pretty straightforward answer given here and elsewhere: you say

EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

Fine then. So I do this and I include the imports:

import android.content.Context;
import android.os.Bundle;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

But when we get to showSoftInput, Studio goes red and says "Cannot resolve symbol showSoftInput". Shouldn't it be getting that symbol when I import the InputMethodManager? showSoftInput doesn't seem to be deprecated or anything.


回答1:


Keyboard is not opening because it needs some delay ,

Follow this code

public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText = (EditText) findViewById(R.id.editText);
       /* */

    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            editText.requestFocus();
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
        }
    },100);
    }
}

and XML

 <EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:ems="10"
    android:focusable="true"
    android:inputType="number"
    android:text="">

    <requestFocus></requestFocus>
</EditText>


来源:https://stackoverflow.com/questions/39593324/cannot-resolve-symbol-showsoftinput

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!