tuve un problema similar con editText, que ganó foco desde que se inició la actividad. este problema i fijo fácil como esto:
que añadir esta pieza de código en la disposición que contiene el EDITTEXT en xml:
android:id="@+id/linearlayout"
android:focusableInTouchMode="true"
no se olvide la android:id
, sin que yo tengo un error.
el otro problema que tuve con editText es que una vez que gana el primer foco, el foco nunca desapareció. este es un pedazo de mi código en Java, tiene un EDITTEXT y un botón que captura el texto en el EDITTEXT:
editText=(EditText) findViewById(R.id.et1);
tvhome= (TextView)findViewById(R.id.tv_home);
etBtn= (Button) findViewById(R.id.btn_homeadd);
etBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
tvhome.setText(editText.getText().toString());
//** this code is for hiding the keyboard after pressing the button
View view = Settings.this.getCurrentFocus();
if (view != null)
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
//**
editText.getText().clear();//clears the text
editText.setFocusable(false);//disables the focus of the editText
Log.i("onCreate().Button.onClickListener()", "et.isfocused= "+editText.isFocused());
}
});
editText.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if(v.getId() == R.id.et1)
{
v.setFocusableInTouchMode(true);// when the editText is clicked it will gain focus again
//** this code is for enabling the keyboard at the first click on the editText
if(v.isFocused())//the code is optional, because at the second click the keyboard shows by itself
{
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT);
}
//**
Log.i("onCreate().EditText.onClickListener()", "et.isfocused= "+v.isFocused());
}
else
Log.i("onCreate().EditText.onClickListener()", "the listener did'nt consume the event");
}
});
esperanza de que ayudará a algunos de ustedes!
No es realmente un duplicado. Esa pregunta es para evitar que EditText se concentre en el inicio de la actividad. Este es sobre quitar el foco. Estos son problemas diferentes. –