我们有一个edittext,默认情况下在点击drawable右侧时禁用,我们需要启用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启用和禁用隐藏/显示键盘。
下面是我们在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);
}
}
答案 0 :(得分:2)
用于显示键盘
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);
<强>更新强>
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;
}
});
答案 1 :(得分:0)
<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.
如果这项工作然后点击右边的符号。
答案 2 :(得分:0)
protected void hideSoftKeyboard() {
if (getCurrentFocus() != null) {
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
}
}
答案 3 :(得分: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) {
}
}
});