下面列出了怎么用android.view.View.OnFocusChangeListener的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Sets the alpha of this FocusIndicatorHelper to 0 when a view with this listener
* receives focus.
*/
public View.OnFocusChangeListener getHideIndicatorOnFocusListener() {
return new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
endCurrentAnimation();
setCurrentView(null);
setAlpha(0);
invalidateDirty();
}
}
};
}
/**
* 获取焦点改变事件监听,设置EditText文本默认全选
* @return 焦点改变事件监听
*/
protected OnFocusChangeListener GetOnFocusChangeListener() {
return new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus && v instanceof EditText) {
((EditText) v).setSelection(0, ((EditText) v).getText().length());
}
}
};
}
private void prepareSearchView() {
final View searchViewLayout =
getLayoutInflater().inflate(R.layout.dialtacts_custom_action_bar, null);
mSearchView = (SearchView) searchViewLayout.findViewById(R.id.search_view);
mSearchView.setOnQueryTextListener(mPhoneSearchQueryTextListener);
mSearchView.setOnCloseListener(mPhoneSearchCloseListener);
// Since we're using a custom layout for showing SearchView instead of letting the
// search menu icon do that job, we need to manually configure the View so it looks
// "shown via search menu".
// - it should be iconified by default
// - it should not be iconified at this time
// See also comments for onActionViewExpanded()/onActionViewCollapsed()
mSearchView.setIconifiedByDefault(true);
mSearchView.setQueryHint(getString(R.string.hint_findContacts));
mSearchView.setIconified(false);
mSearchView.setOnQueryTextFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View view, boolean hasFocus) {
if (hasFocus) {
showInputMethod(view.findFocus());
}
}
});
if (!ViewConfiguration.get(this).hasPermanentMenuKey()) {
// Filter option menu should be shown on the right side of SearchView.
final View filterOptionView = searchViewLayout.findViewById(R.id.search_option);
filterOptionView.setVisibility(View.VISIBLE);
filterOptionView.setOnClickListener(mFilterOptionClickListener);
}
getActionBar().setCustomView(searchViewLayout,
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}
public View.OnFocusChangeListener getOnFocusChangeListener()
{
if (_onFocusChangeListener == null)
{
_onFocusChangeListener = getDecorView().getOnFocusChangeListener();
}
return _onFocusChangeListener;
}
void setIconFocusListener(OnFocusChangeListener focusListener) {
mIconFocusListener = focusListener;
}
public OnFocusChangeListener getFocusListener() {
return mHelper;
}
@Override
public void setOnFocusChangeListener(OnFocusChangeListener f) {
this.f = f;
}
@Override
public void setOnFocusChangeListener(OnFocusChangeListener f) {
this.f = f;
}