当checkBox被选中时,在editText中直接显示键盘

时间:2016-02-19 03:59:44

标签: android checkbox android-edittext spinner

所以我在editText中有checkboxspinneractivity

选中checkbox后,系统会显示editText并隐藏spinner

 public void addListenerOnTo() // for checkbox
    {

        checkBoxTo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    travelTo.setVisibility(View.INVISIBLE); // spinner
                    addTo.setVisibility(View.VISIBLE); // editText

                } else {
                    travelTo.setVisibility(View.VISIBLE);
                    addTo.setVisibility(View.INVISIBLE);

                }
            }
        });
    }

如果选中checkbox而未按editText显示键盘,我该如何直接输入输入?感谢。

2 个答案:

答案 0 :(得分:1)

这可以通过将焦点设置在edittext上然后强制显示软键盘来实现!

  public void addListenerOnTo() // for checkbox
        {

            checkBoxTo.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        travelTo.setVisibility(View.INVISIBLE); // spinner
                        addTo.setVisibility(View.VISIBLE); // editText

    // add this line to make the keyboard visible 

addTo.requestFocus();

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

                    } else {
                        travelTo.setVisibility(View.VISIBLE);
                        addTo.setVisibility(View.INVISIBLE);

                    }
                }
            });
        }

让我知道这是否有效! :)

答案 1 :(得分:0)

extend

之后添加此代码
addTo.setVisibility(View.VISIBLE);