如何基于EditText启用和禁用并处理键盘提交的方式隐藏/显示键盘



我们有一个可持续下文,默认情况下,单击可绘制的权利,我们需要启用editext。禁用时,我们使用了带有相对布局的图像按钮,如下所述。

<?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>

如何基于EDITTEXT启用和禁用隐藏/显示键盘。

以下是我们在查看holder类中使用的代码recyclerview

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

}

显示键盘

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

为隐藏键盘。(单击完成按钮时,这将隐藏键盘(

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.

如果这项工作,请单击右符号。

 protected void hideSoftKeyboard() {
        if (getCurrentFocus() != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
        }
    }

尝试这个。

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

最新更新