Miałem podobny problem z tekstem editText, który uzyskał fokus od rozpoczęcia aktywności. Ten problem mam stałe łatwo tak:
dodać ten kawałek kodu w układzie zawierającym EditText w XML:
android:id="@+id/linearlayout"
android:focusableInTouchMode="true"
nie zapomnieć android:id
, bez niego mam błąd.
Innym problemem, jaki miałem z editText, jest to, że gdy tylko uzyska pierwsze skupienie, ostrość nigdy nie zniknie. Jest to kawałek mojego kodu w Javie, ma EditText i przycisk, który przechwytuje tekst w 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");
}
});
nadzieję, że przyczyni się do niektórych z was!
W rzeczywistości nie jest duplikatem. Pytanie to polega na zapobieganiu koncentracji EditText na uruchamianiu aktywności. Ten dotyczy usuwania ostrości. To są różne problemy. –