【问题标题】:How to open Keyboard programmatically when move from one activity从一项活动移动时如何以编程方式打开键盘
【发布时间】:2013-01-31 07:40:12
【问题描述】:

当我从第一个活动转到第二个活动时,我正在尝试打开键盘。 主要活动中有两个按钮 1)如果单击“NotShowKeyboard”按钮,它将打开没有键盘的 second.java 活动 2) 如果单击 s"ShowKeyboard" 按钮,那么它将打开带有键盘和焦点的 EitdText 的 second.java 活动 但问题是我不知道该怎么做。我放了一些示例顶部显示键盘,但在“ShowKeyboard”按钮上单击键盘打开并立即消失。

Main.java:

    NotShowKeyboard.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
            }

            if (event.getAction() == MotionEvent.ACTION_UP) {
                bundle.putBoolean("show", false);
                Intent start = new Intent(Main.this, Start.class);
                start.putExtras(bundle);
                startActivity(start);
            }

            return false;
        }
    });

    ShowKeyboard.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
            }

            if (event.getAction() == MotionEvent.ACTION_UP) {
                bundle.putBoolean("show", true);
                Intent start = new Intent(Main.this, Start.class);
                start.putExtras(bundle);
                startActivity(start);
            }

            return false;
        }
    });

Second.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
    setContentView(R.layout.start);
    i = getIntent();
    extras = i.getExtras();
    search = (EditText) findViewById(R.id.start_edit);
    search.addTextChangedListener(myTextWatcher);
    if((extras.getBoolean("show"))==true) {
    ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
    //getting all stuff like buttons imageViews etc.. 
}

当点击 NotShowButton 时应该会打开:

当 ShowButton Clicked 时应该会打开:

【问题讨论】:

    标签: android android-softkeyboard


    【解决方案1】:

    隐藏键盘:

    InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
    

    显示键盘:

    InputMethodManager imm = (InputMethodManager)   getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    

    【讨论】:

    • 我发现 InputMethodManager.SHOW_IMPLICIT 的行为更可取。例如,当您按下主页按钮时,我会关闭键盘。
    【解决方案2】:

    你试过吗?

    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    

    尝试将此代码放入onResume()

    【讨论】:

    • 如果尝试10次后才有效,那不是解决方案;)
    【解决方案3】:

    有时它取决于设备,软键盘是否出现。在我的 Vodafone Smart II 手机上,在我的 Intexo TAB814(8" 显示屏)上没有出现它(3,5" 显示屏),它在那里。相同的应用程序,但不同的结果。它也似乎取决于您使用的 android 版本。 (Vodafone smart II -> 2.1.3, Intenso tablet 4.1) 在其他设备上试试

    【讨论】:

    • 当时我的设备没有任何问题。问题是键盘显示但 1 秒后立即消失。
    • 'if((extras.getBoolean("show"))==true) { ((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); }' 是你的keyboard=true 按钮,是吗?然后尝试使用“SHOW_IMPLICIT”而不是“HIDE_IMPLICIT_ONLY”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多