简体   繁体   English

如何基于edittext启用/禁用隐藏/显示键盘以及处理键盘提交

[英]How to hide/show keyboard based on edittext enable and disable and also handle keyboard submit

We have a edittext which is disable by default on click of drawable right we need to enable the editext. 我们有一个edittext,默认情况下,在单击drawable权限时将其禁用,我们需要启用editext。 when it is disable so we have used a image button with relative layout as mentioned below. 当它被禁用时,我们使用了具有相对布局的图像按钮,如下所述。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    >

        <android.support.design.widget.TextInputLayout
            android:id="@+id/usernameWrapper"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp">

                <EditText
                    android:id="@+id/username"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textEmailAddress"
                    android:hint="Username"/>

        </android.support.design.widget.TextInputLayout>
        <ImageButton
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:src="@mipmap/edit_icon"
            android:layout_centerVertical="true"
            android:layout_margin="5dp"
            android:text="Button"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
                android:background="@color/card_header"
            android:layout_below="@+id/usernameWrapper"/>

</RelativeLayout>

How to hide/show keyboard based on edittext enable and disable. 如何基于edittext启用和禁用隐藏/显示键盘。

below is code we are using in view holder class of recyclerview 下面是我们在recyclerview的viewholder类中使用的代码

public class CountItemViewHolder extends RecyclerView.ViewHolder {
    EditText textView;
    View containerView;
    Context context;
    Boolean flag=false;
    ImageButton button1;
    public CountItemViewHolder(View itemView, Context context) {
        super(itemView);
        textView = (EditText) itemView.findViewById(R.id.username);
        containerView = (View) itemView.findViewById(R.id.container);
        button1 = (ImageButton) itemView.findViewById(R.id.button1);
        this.context = context;
    }
    public void render(String text){
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("click hua");
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                if(!flag) {

                    button1.setImageResource(R.mipmap.checked_icon);
                    textView.setEnabled(true);
                    textView.setFocusable(true);
                    textView.setFocusableInTouchMode(true);
                    textView.setCursorVisible(true);
                    textView.setInputType(InputType.TYPE_CLASS_TEXT); // disable soft input

                    textView.requestFocus();
                    imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT);
                    flag = true;
                }else{
                    button1.setImageResource(R.mipmap.edit_icon);
                    imm.hideSoftInputFromWindow(textView.getWindowToken(), 0);

                    disableEditText(textView);



                    flag = false;

                }
            }
        });
        textView.setOnEditorActionListener(new EditText.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    System.out.println("keyboard close");
                    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);

                    imm.hideSoftInputFromWindow(textView.getWindowToken(), 0);

                    disableEditText(textView);

                    return true;
                }
                return false;
            }
        });
        textView.setText(text);
        disableEditText(textView);
    }
    private void disableEditText(EditText editText) {
       editText.setFocusable(false);
        editText.setEnabled(false);
        editText.setCursorVisible(false);
        editText.setFocusableInTouchMode(false);

        editText.setBackgroundColor(Color.TRANSPARENT);
    }


}

For Show the Keyboard 用于显示键盘

InputMethodManager keyboard = (InputMethodManager)
            getSystemService(Context.INPUT_METHOD_SERVICE);
            keyboard.showSoftInput(yourEdittext, 0);

For Hide the keyboard.(This will hide the keyboard when you click done button) 对于隐藏键盘。(单击完成按钮时,它将隐藏键盘)

InputMethodManager keyboard = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(yourEdittext.getView().getWindowToken(), 0);

UPDATE 更新

yourEdittext.setOnKeyListener(new View.OnKeyListener() {
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
            yourEdittext.setEnable(false);
            return true;
        }
        return false;
    }
});
<activity
            android:name=".ActivityDemo"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@style/AppTheme.NoActionBar"
            android:windowSoftInputMode="stateHidden|adjustPan" />

Use in menifest file.

if this work then click on right symbol. 如果这项工作,然后单击正确的符号。

 protected void hideSoftKeyboard() {
        if (getCurrentFocus() != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);

            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    }

Try this. 尝试这个。

your_button_id.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        try  {
            InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        } catch (Exception e) {

        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM