在编辑文本时如何禁用弹出式小键盘?

在我的应用程序中,我所有屏幕的第一个视图是一个 EditText,所以每次我走到一个屏幕,屏幕上的键盘就会弹出来。我如何禁用这个弹出式窗口,并启用它时,手动点击编辑文本? ? ? ?

    eT = (EditText) findViewById(R.id.searchAutoCompleteTextView_feed);


eT.setOnFocusChangeListener(new OnFocusChangeListener() {


public void onFocusChange(View v, boolean hasFocus) {


if(hasFocus){
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(eT.getWindowToken(), 0);
}
}
});

Xml 代码:

<ImageView
android:id="@+id/feedPageLogo"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/wic_logo_small" />


<Button
android:id="@+id/goButton_feed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="@string/go" />


<EditText
android:id="@+id/searchAutoCompleteTextView_feed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/goButton_feed"
android:layout_toRightOf="@id/feedPageLogo"
android:hint="@string/search" />


<TextView
android:id="@+id/feedLabel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/feedPageLogo"
android:gravity="center_vertical|center_horizontal"
android:text="@string/feed"
android:textColor="@color/white" />


<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ButtonsLayout_feed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >


<Button
android:id="@+id/feedButton_feed"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_margin="0dp"
android:layout_weight="1"
android:background="@color/white"
android:text="@string/feed"
android:textColor="@color/black" />


<Button
android:id="@+id/iWantButton_feed"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_margin="0dp"
android:layout_weight="1"
android:background="@color/white"
android:text="@string/iwant"
android:textColor="@color/black" />


<Button
android:id="@+id/shareButton_feed"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_margin="0dp"
android:layout_weight="1"
android:background="@color/white"
android:text="@string/share"
android:textColor="@color/black" />


<Button
android:id="@+id/profileButton_feed"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_margin="0dp"
android:layout_weight="1"
android:background="@color/white"
android:text="@string/profile"
android:textColor="@color/black" />
</LinearLayout>


<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/feedListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/ButtonsLayout_feed"
android:layout_below="@id/feedLabel"
android:textSize="15dp" >
</ListView>

第三个视图(EditText)是焦点所在。

156301 次浏览

You can use following code for disabling OnScreen Keyboard.

InputMethodManager im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(editText.getWindowToken(), 0);

Try With this:

EditText yourEditText= (EditText) findViewById(R.id.yourEditText);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

To close, you can use:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(yourEditText.getWindowToken(), 0);

Try it like this in your code:

ed = (EditText)findViewById(R.id.editText1);


InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(ed.getWindowToken(), 0);


ed.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(ed, InputMethodManager.SHOW_IMPLICIT);
}
});

Declare the global variable for InputMethodManager:

 private InputMethodManager im ;

Under onCreate() define it:

 im = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.hideSoftInputFromWindow(youredittext.getWindowToken(), 0);

Set the onClickListener to that edit text inside oncreate():

 youredittext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
im.showSoftInput(youredittext, InputMethodManager.SHOW_IMPLICIT);
}
});

This will work.

You have to create a view, above the EditText, that takes a 'fake' focus:

Something like :

<!-- Stop auto focussing the EditText -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@android:color/transparent"
android:focusable="true"
android:focusableInTouchMode="true">
</LinearLayout>


<EditText
android:id="@+id/searchAutoCompleteTextView_feed"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:inputType="text" />

In this case, I used a LinearLayout to request the focus. Hope this helps.

This worked perfectly...thanks to Zaggo0

Use the following code, write it under onCreate()

InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

The best solution lies in the Project Manifest file (AndroidManifest.xml), add the following attribute in the activity construct

android:windowSoftInputMode="stateHidden"


Example:

    <activity android:name=".MainActivity"
android:windowSoftInputMode="stateHidden" />

Description:

  • The state of the soft keyboard — whether it is hidden or visible — when the activity becomes the focus of user attention.
  • The adjustment made to the activity's main window — whether it is resized smaller to make room for the soft keyboard or whether its contents pan to make the current focus visible when part of the window is covered by the soft keyboard.

Introduced in:

  • API Level 3.

Link to the Docs

Note: Values set here (other than "stateUnspecified" and "adjustUnspecified") override values set in the theme.

Use following code in your onCreate() method-

    editText = (EditText) findViewById(R.id.editText);
editText.requestFocus();
editText.postDelayed(new Runnable() {
public void run() {
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(
editText.getWindowToken(), 0);
}
}, 200);

Add below code on your activity class.

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Keyboard will pop-upped when user clicks on the EditText

     edittext.setShowSoftInputOnFocus(false);

Now you can use any custom keyboard you like.

try it.......i solve this problem using code:-

EditText inputArea;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inputArea = (EditText) findViewById(R.id.inputArea);


//This line is you answer.Its unable your click ability in this Edit Text
//just write
inputArea.setInputType(0);
}

nothing u can input by default calculator on anything but u can set any string.

try it

In a Android app I was building, I had three EditTexts in a LinearLayout arranged horizontally. I had to prevent the soft keyboard from appearing when the fragment loaded. In addition to setting focusable and focusableInTouchMode to true on the LinearLayout, I had to set descendantFocusability to blocksDescendants. In onCreate, I called requestFocus on the LinearLayout. This prevented the keyboard from appearing when the fragment is created.

Layout -

    <LinearLayout
android:id="@+id/text_selector_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="3"
android:orientation="horizontal"
android:focusable="true"
android:focusableInTouchMode="true"
android:descendantFocusability="blocksDescendants"
android:background="@color/black">


<!-- EditText widgets -->


</LinearLayout>

In onCreate - mTextSelectorContainer.requestFocus();

Two Simple Solutions:

First Solution is added below line of code in manifest xml file. In Manifest file (AndroidManifest.xml), add the following attribute in the activity construct

android:windowSoftInputMode="stateHidden"

Example:

<activity android:name=".MainActivity"
android:windowSoftInputMode="stateHidden" />

Second Solution is adding below line of code in activity

//Block auto opening keyboard
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

We can use any one solution of above. Thanks

for any textview at your activity (or create fake empty textfiew with android:layout_width="0dp" android:layout_height="0dp" ) and add for this textview next: android:textIsSelectable="true"

People have suggested many great solutions here, but I used this simple technique with my EditText (nothing in java and AnroidManifest.xml is required). Just set your focusable and focusableInTouchMode to false directly on EditText.

 <EditText
android:id="@+id/text_pin"
android:layout_width="136dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textAlignment="center"
android:inputType="numberPassword"
android:password="true"
android:textSize="24dp"
android:focusable="false"
android:focusableInTouchMode="false"/>

My intent here is to use this edit box in App lock activity where I am asking user to input the PIN and I want to show my custom PIN pad. Tested with minSdk=8 and maxSdk=23 on Android Studio 2.1

enter image description here

Only thing you need to do is that add android:focusableInTouchMode="false" to the EditText in xml and thats all.(If someone still needs to know how to do that with the easy way)

If anyone is still looking for the easiest solution, set the following attribute to true on your parent layout

android:focusableInTouchMode="true"

Example:

<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true">


.......
......
</android.support.constraint.ConstraintLayout>

Use This to enable and disable EditText....

InputMethodManager imm;


imm = (InputMethodManager)
getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);


if (isETEnable == true) {


imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
ivWalllet.setImageResource(R.drawable.checkbox_yes);
etWalletAmount.setEnabled(true);
etWalletAmount.requestFocus();
isETEnable = false;
}
else {


imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY,0);
ivWalllet.setImageResource(R.drawable.checkbox_unchecked);
etWalletAmount.setEnabled(false);
isETEnable = true;
}

For Xamarin Users:

[Activity(MainLauncher = true,
ScreenOrientation = ScreenOrientation.Portrait,
WindowSoftInputMode = SoftInput.StateHidden)] //SoftInput.StateHidden - disables keyboard autopop

Try this answer,

editText.setRawInputType(InputType.TYPE_CLASS_TEXT);
editText.setTextIsSelectable(true);

Note: only for API 11+

private InputMethodManager imm;


...




editText.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
v.onTouchEvent(event);
hideDefaultKeyboard(v);
return true;
}
});


private void hideDefaultKeyboard(View et) {
getMethodManager().hideSoftInputFromWindow(et.getWindowToken(), 0);
}


private InputMethodManager getMethodManager() {
if (this.imm == null) {
this.imm = (InputMethodManager)  getContext().getSystemService(android.content.Context.INPUT_METHOD_SERVICE);
}
return this.imm;
}

Well, I had the same problem and I just tackled with focusable in the XML file.

<EditText
android:cursorVisible="false"
android:id="@+id/edit"
android:focusable="false"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

You probably are looking for security also. This will help in that also.

       <TextView android:layout_width="match_parent"
android:layout_height="wrap_content"


android:focusable="true"
android:focusableInTouchMode="true">


<requestFocus/>
</TextView>


<EditText android:layout_width="match_parent"
android:layout_height="wrap_content"/>

If you are using Xamarin you can add this

Activity[(WindowSoftInputMode = SoftInput.StateAlwaysHidden)]

thereafter you can add this line in OnCreate() method

youredittext.ShowSoftInputOnFocus = false;

If the targeted device does not support the above code, you can use the code below in EditText click event

InputMethodManager Imm = (InputMethodManager)this.GetSystemService(Context.InputMethodService);
Imm.HideSoftInputFromWindow(youredittext.WindowToken, HideSoftInputFlags.None);

I found the following pattern to work well for me in code where I want to show a dialog to get the input (e.g., the string displayed in the text field is the result of selections made from a list of checkboxes in a dialog, rather than text entered via the keyboard).

  1. Disable soft input focus on the edit field. I can't disable for the entire activity, as there are edit fields I do want the keyboard to be used for in the same layout.
  2. Initial clicks in the text field yield a focus change, a repeated click yields a click event. So I override both (here I don't refactor the code out to illustrate both handlers do the same thing):

    tx = (TextView) m_activity.findViewById(R.id.allergymeds);
    if (tx != null) {
    tx.setShowSoftInputOnFocus(false);
    tx.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean hasFocus) {
    if (hasFocus) {
    MedicationsListDialogFragment mld = new MedicationsListDialogFragment();
    mld.setPatientId(m_sess.getActivePatientId());
    mld.show(getFragmentManager(), "Allergy Medications Dialog");
    }
    }
    });
    tx.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    MedicationsListDialogFragment mld = new MedicationsListDialogFragment();
    mld.setPatientId(m_sess.getActivePatientId());
    mld.show(getFragmentManager(), "Allergy Medications Dialog");
    }
    });
    }
    

Thanks @A.B for good solution

    android:focusableInTouchMode="false"

this case if you will disable keyboard in edit text , just add android:focusableInTouchMode="false" in edittext tagline.

work for me in Android Studio 3.0.1 minsdk 16 , maxsdk26

Just remove the <request focus/> tag from the xml file.

Just need to add property android:focusable="false" in particular EditText in the layout xml. Then you can write click listner for EditText without keypad popup.

It issue can be sorted by using, No need to set editText inputType to any values, just add the below line, editText.setTextIsSelectable(true);

Try below lines of code when you want to hide keyboard on screen on any event. It worked for me.

context.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN );


final InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);


if (inputMethodManager.isAcceptingText())       inputMethodManager.hideSoftInputFromWindow(context.getCurrentFocus().getWindowToken(), 0);