我有一个类似的问题,editText自从活动开始后就获得了关注。这个问题我很容易固定这样的:
添加这段代码到包含在XML中EDITTEXT布局:
android:id="@+id/linearlayout"
android:focusableInTouchMode="true"
不要忘了android:id
,没有它,我已经有了一个错误。
我用editText时遇到的另一个问题是,一旦获得第一个焦点,焦点就不会消失。这是一张我在Java代码中,它捕获的EDITTEXT文字的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");
}
});
希望这将有助于一些你!
实际上并不重复。这个问题是关于防止EditText获得关注活动启动的问题。这一个是关于消除焦点。这些是不同的问题。 – 2014-07-12 18:19:21