我有一個類似的問題,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