First of all, I have seen the questions on this topic but no answer has helped me, so I decided to open a new one.
I want the keyboard to close whenever I click outside of it, it does not matter if I do it in a TextView
, ImageView
... or an empty place.
This has worked for me but I have to double click if I do it above ScrollView
:
@Override
public boolean onTouchEvent(MotionEvent event) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.
INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
return true;
}
Can someone help me?
clase
:
public class Prueba extends AppCompatActivity implements SimpleGestureListener {
TextView TextView1;
GestosActivity detector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prueba);
// Oculta teclado al iniciar la Activity
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
TextView1 = (TextView) findViewById(R.id.TextView1);
// gestos
detector = new GestosActivity(this, this);
// cierra teclado al hacer clic en TextView1
TextView1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), 0);
return false;
}
});
}
// gestos
@Override
public boolean dispatchTouchEvent(MotionEvent me) {
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
@Override
public void onSwipe(int direction) {
switch (direction) {
case GestosActivity.SWIPE_DOWN:
Toast.makeText(this, "Sin función de momento", Toast.LENGTH_LONG).show();
break;
case GestosActivity.SWIPE_UP:
Toast.makeText(this, "Sin función de momento", Toast.LENGTH_LONG).show();
}
}
}
Layout
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/base_añadir"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#f4f4f4">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:gravity="top|center"
android:orientation="vertical">
<EditText
android:id="@+id/editTextNombre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
<TextView
android:id="@+id/TextView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</ScrollView>
</AbsoluteLayout>
EDITO1:
public class Prueba extends AppCompatActivity implements SimpleGestureListener {
TextView TextView1;
GestosActivity detector;
EditText editTextNombre;
View view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.prueba);
// Oculta teclado al iniciar la Activity
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
editTextNombre = (EditText) findViewById(R.id.editTextNombre);
TextView1 = (TextView) findViewById(R.id.TextView1);
// gestos
detector = new GestosActivity(this, this);
editTextNombre.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
});
}
// gestos
@Override
public boolean dispatchTouchEvent(MotionEvent me) {
this.detector.onTouchEvent(me);
return super.dispatchTouchEvent(me);
}
@Override
public void onSwipe(int direction) {
switch (direction) {
case GestosActivity.SWIPE_DOWN:
Toast.makeText(this, "Sin función de momento", Toast.LENGTH_LONG).show();
break;
case GestosActivity.SWIPE_UP:
Toast.makeText(this, "Sin función de momento", Toast.LENGTH_LONG).show();
}
}
}