Reputation: 101
I have an activity where there are edit text, but I have a problem because the virtual keyboard automatically appears.
I wonder if there is not a way that it does not automatically appears but only when you click on a Edit Text
Upvotes: 0
Views: 168
Reputation: 5971
You need to set your EditText to respond to focus change event, and hide keyboard manually,
public class Activity1 extends Activity implements OnFocusChangeListener
{
protected void onCreate( Bundle b )
{
.....
txtX = (EditText) findViewById(R.id.text_x);
txtX.setOnFocusChangeListener(this);
}
public void hideKeyboard(View view)
{
InputMethodManager inputMethodManager =(InputMethodManager)context.getSystemService(Activity.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
@Override
public void onFocusChange(View view, boolean arg1)
{
if(! view.hasFocus())
hideKeyboard(view);
}
}
and in xml set your layout to focusable
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:focusableInTouchMode="true" >
<EditText
android:id="@+id/text_x"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Upvotes: 0
Reputation: 3349
Try this
private void showKeyboard(View view) {
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(view, 0);
}
private void hideKeyboard() {
InputMethodManager inputMethodManager = (InputMethodManager) this
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus()
.getWindowToken(), 0);
}
Upvotes: 0
Reputation: 9700
Write the following code in onResume() method then the keyboard will not popup automatically...
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0);
Upvotes: 0
Reputation: 20307
Just add in the Manifest for your activity: android:windowSoftInputMode="stateHidden"
. For example:
<activity
android:name="com.proj.activity.MainActivity"
android:windowSoftInputMode="stateHidden" />
Upvotes: 0
Reputation: 13520
You can use
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
in your Activity
. The keyboard will only open when you click on it
Upvotes: 1