在xml文件中,edittext這個控件設置了,android:focusable=“false”,
在代碼中edittext.setfocusable(true),edittext這個控件仍然無法點擊,百度之後,得到的結果是:
一定要按一下這個順序寫,重新使edittext獲取焦點:
edittext.setFocusable(true);
edittext.setFocusableInTouchMode(true);
edittext.requestFocus();
edittext.requestFocusFromTouch();
這是爲什麼呢?
查閱API:
setFocusable(boolean):
Set whether this view can receive the focus. Setting this to false will also ensure that this view is not focusable in touch mode.
//--設置edittext是否可以獲得焦點
setFocusableInTouchMode(boolean):
Set whether this view can receive focus while in touch mode. Setting this to true will also ensure that this view is focusable.
//--設置edittext在touch模式下是否可以獲得焦點
requestFocus():
Call this to try to give focus to a specific view or to one of its descendants. A view will not actually take focus if it is not focusable (isFocusable()
returns false), or if it is focusable and it is not focusable
in touch mode (isFocusableInTouchMode()
) while the device is in touch mode.
//--調用這個給指定的view或者它的子view焦點。如果這個view在isFocusable()方法下返回false,或者isFocusableInTouchMode()方法下返回false,這個view不會真正獲得焦點
requestFocusFromTouch():
Call this to try to give focus to a specific view or to one of its descendants. This is a special variant of
requestFocus()
that will allow views that are not focuable in touch mode to request focus when they are touched.
//--調用這個給指定的view或者它的子view焦點。這個特別的變異於(?)requestFocus()的方法會讓在這個view被touch時,給view焦點。
以上。