Android:如何在单击按钮时打开用于编辑EditText的键盘?

6

我的情况是:我有一个禁用焦点的EditText字段。除了EditText字段,我还有两个输入法按钮。因此,当我点击第一个按钮时,我希望打开软键盘并编辑EditText字段中的文本。我尝试了很多方法:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

对我不起作用。唯一打开软键盘的方法是:

toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)

但没有办法从EditText字段中编辑信息。

你能建议我如何在点击按钮时打开键盘并编辑某些EditText中的文本吗? 非常感谢!

编辑:

这样,EditText默认情况下是无法获得焦点的。当我点击键盘按钮时 - 应该是可获得焦点的,然后显示软键盘以输入文本并出现在EditText中。插入的另一种方法是不需要键盘的A-B-C按钮。它将是类似于摩尔斯电码输入的东西 - 触摸并按住A-B-C按钮 :) 我会尝试建议的示例来实现我的情况。谢谢!

my case


你的 EditText 为什么一开始就不能聚焦?你是如何实现的?使用 XML 还是具体用了哪些属性? - andr
1
因为这个编辑文本框将是非正常的编辑文本框。当您点击键盘按钮并显示软键盘或我稍后实现的其他方式时,将输入文本。我在XML中以这种方式禁用它:android:focusable="false" - Dodo
7个回答

24

感谢大家的帮助:)我使用了你们所提供的所有建议,搜索并测试了很多其他脚本,最终我的代码运行成功了:)

以下是我的最终代码:

InputEditText = (EditText) findViewById(R.id.InputText);

public void InputData() {

        /* Keyboard Button Action */
        KeyboardButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Log.v(TAG, "On Keyboard Button click event!");

                InputEditText.requestFocus();
                InputEditText.setFocusableInTouchMode(true);

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(InputEditText, InputMethodManager.SHOW_FORCED);

            }

        });

}

这可能对某些人有用 :) 谢谢!


在你的情况下,是哪个键盘按钮? - Sujay U N
@Dodo,我发现这对我的应用程序非常有帮助,非常感谢你,伙计,继续保持。 - TheCoderGuy

13

你所描述的设计不被推荐。你正在违反 focusable 属性的目的,该属性的作用并不是控制用户是否可以更改 EditText 组件中的文本。

如果您计划提供替代输入方法,因为使用情况似乎需要这样做(例如,您只允许在可编辑文本字段中输入特定集合的符号),那么您应该在用户不被允许更改该字段的值的时间内完全禁用文本编辑。

声明您的可编辑字段:

<EditText
    android:id="@+id/edit_text_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />

请注意,focusable属性仍保留默认值。没关系,稍后我们会处理它。声明一个按钮,它将启动编辑过程:

<Button
    android:id="@+id/button_show_ime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start editing" />

现在,在你的Activity中声明:

private EditText editText2;
private KeyListener originalKeyListener;
private Button buttonShowIme;

onCreate()中做如下操作:

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

    // Find out our editable field.
    editText2 = (EditText)findViewById(R.id.edit_text_2);
    // Save its key listener which makes it editable.
    originalKeyListener = editText2.getKeyListener();
    // Set it to null - this will make the field non-editable
    editText2.setKeyListener(null);

    // Find the button which will start editing process.
    buttonShowIme = (Button)findViewById(R.id.button_show_ime);
    // Attach an on-click listener.
    buttonShowIme.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Restore key listener - this will make the field editable again.
            editText2.setKeyListener(originalKeyListener);
            // Focus the field.
            editText2.requestFocus();
            // Show soft keyboard for the user to enter the value.
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(editText2, InputMethodManager.SHOW_IMPLICIT);
        }
    });

    // We also want to disable editing when the user exits the field.
    // This will make the button the only non-programmatic way of editing it.
    editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // If it loses focus...
            if (!hasFocus) {
                // Hide soft keyboard.
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(editText2.getWindowToken(), 0);
                // Make it non-editable again.
                editText2.setKeyListener(null);
            }
        }
    });
}

我希望代码及其注释都是自说明的。在 API 8 和 17 上测试通过。


1
我明白了。你可以试着实现它,如果有问题的话,也许我能想办法修复成你想要的效果。但是不要使用“focusable”属性——这意味着用户将无法复制字段内容,这会破坏 UI 的自然功能。用户不喜欢受到这样的限制。 - andr
非常感谢您的建议 :) 最终我通过您和其他一些示例完成了它。将在答案中分享我的代码。 - Dodo

5

尝试这个:

        final EditText myedit2 = (EditText) findViewById(R.id.myEditText2);

        Button btsmall = (Button) findViewById(R.id.BtSmall);
        btsmall.setOnClickListener(new OnClickListener() {              
            @Override
            public void onClick(View arg0) {
                myedit2.requestFocus();
            }
        });

1

对于使用片段的人,可以这样使用InputMethodManager

InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
            }

完整代码:

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                editText.setFocusable(true);
                editText.setFocusableInTouchMode(true);
                editText.requestFocus();
                InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
                }
            }
        });

1
LinearLayout ll_about_me =(LinearLayout) view.findViewById(R.id.ll_about_me);
    ll_about_me.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

            mEtEmpAboutYou.requestFocus();
            mEtEmpAboutYou.setFocusableInTouchMode(true);

            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(mEtEmpAboutYou, InputMethodManager.SHOW_FORCED);

            return true;
        }
    });

1

我已经编写了这段代码,可以在按钮点击时打开键盘。 类似于这样。

btn1 = (Button) findViewById(R.id.btn1);
edt1 = (EditText) findViewById(R.id.edt1);
btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            edt1.requestFocus();
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(edt1, InputMethodManager.SHOW_IMPLICIT);
        }
    });

它是完成的工作。

0

希望这可以帮到你

EditText txt_categorie = findViewById(R.id.txt_categorie);
txt_categorie.requestFocus();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接