Android:点击按钮时如何打开编辑EditText的键盘?

问题描述 投票:5回答: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按钮:)我将尝试在我的情况下实施的建议示例。感谢你们 :)

android button android-edittext android-softkeyboard
6个回答
15
投票

谢谢你的帮助:)我使用了你给我的所有建议,搜索并测试了很多其他脚本,最后我的代码正在运行:)

这是我的最终代码:

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);

            }

        });

}

它可能对某人有用:)谢谢!


12
投票

不推荐您描述的设计。您违反了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上测试过。


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
投票

我已经使用此代码在按钮点击时打开键盘。喜欢 。

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);
        }
    });

完成的工作。


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;
        }
    });

0
投票

对于那些使用片段的人,你可以这样使用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);
                }
            }
        });
© www.soinside.com 2019 - 2024. All rights reserved.