在 android TV Leanback 中隐藏遥控器后退按钮的键盘
hide keyboard from remote's back button in android TV Leanback
我试图在按下后退按钮时隐藏键盘,但实际上在按下后退按钮时它返回到上一个菜单。我拥有的是一个带有 SupportSearchFragment 的简单 activity。
public class SearchActivity extends LeanbackActivity {
private SearchFragment mFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
mFragment = (SearchFragment) getSupportFragmentManager()
.findFragmentById(R.id.search_fragment);
}
@Override
public boolean onSearchRequested() {
mFragment.startRecognition();
return true;
}
}
片段是
public class SearchFragment extends SearchSupportFragment
implements SearchSupportFragment.SearchResultProvider {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setSearchResultProvider(this);
}
@Override
public ObjectAdapter getResultsAdapter() {
return null;
}
@Override
public boolean onQueryTextChange(String newQuery) {
loadQuery(newQuery);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
loadQuery(query);
return true;
}
}
我正在寻找的是,一旦按下后退按钮,它应该隐藏键盘,然后再次按下后退按钮,它应该转到上一个菜单。
我发现,如果 ArrayObjectAdapter
有任何要显示的项目,则后退按钮会直接转到上一个菜单。但是,如果没有项目,它会隐藏键盘。
有什么建议吗?提前致谢。
首先覆盖 onBackPressed
,ref
override fun onBackPressed(){
if( view.keyboardIsVisible() )
this.hideKeyboard()
else
super.onBackPressed(); // optional depending on your needs
}
实施您的 KeyIsVisible()
, ref
fun View.keyboardIsVisible() = WindowInsetsCompat
.toWindowInsetsCompat(rootWindowInsets)
.isVisible(WindowInsetsCompat.Type.ime())
并通过
隐藏键盘
/**
* Use only from Activities, don't use from Fragment (with getActivity) or from Dialog/DialogFragment
*/
fun Activity.hideKeyboard() {
val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
val view = currentFocus ?: View(this)
imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
Ps ,当我看到你的语言是 java 时为时已晚,我已附上参考文献,这将帮助你转换。
感谢@Anshul 的回答。我将正是我所需要的,因为后退按钮总是隐藏键盘。
在Activity
override fun onBackPressed() {
if (fragment?.isKeyBoardVisible() != true) super.onBackPressed()
}
片段
fun isKeyBoardVisible() = ViewCompat.getRootWindowInsets(requireView())?.isVisible(WindowInsetsCompat.Type.ime()) ?: true
我试图在按下后退按钮时隐藏键盘,但实际上在按下后退按钮时它返回到上一个菜单。我拥有的是一个带有 SupportSearchFragment 的简单 activity。
public class SearchActivity extends LeanbackActivity {
private SearchFragment mFragment;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
mFragment = (SearchFragment) getSupportFragmentManager()
.findFragmentById(R.id.search_fragment);
}
@Override
public boolean onSearchRequested() {
mFragment.startRecognition();
return true;
}
}
片段是
public class SearchFragment extends SearchSupportFragment
implements SearchSupportFragment.SearchResultProvider {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setSearchResultProvider(this);
}
@Override
public ObjectAdapter getResultsAdapter() {
return null;
}
@Override
public boolean onQueryTextChange(String newQuery) {
loadQuery(newQuery);
return true;
}
@Override
public boolean onQueryTextSubmit(String query) {
loadQuery(query);
return true;
}
}
我正在寻找的是,一旦按下后退按钮,它应该隐藏键盘,然后再次按下后退按钮,它应该转到上一个菜单。
我发现,如果 ArrayObjectAdapter
有任何要显示的项目,则后退按钮会直接转到上一个菜单。但是,如果没有项目,它会隐藏键盘。
有什么建议吗?提前致谢。
首先覆盖 onBackPressed
,ref
override fun onBackPressed(){
if( view.keyboardIsVisible() )
this.hideKeyboard()
else
super.onBackPressed(); // optional depending on your needs
}
实施您的 KeyIsVisible()
, ref
fun View.keyboardIsVisible() = WindowInsetsCompat
.toWindowInsetsCompat(rootWindowInsets)
.isVisible(WindowInsetsCompat.Type.ime())
并通过
隐藏键盘/**
* Use only from Activities, don't use from Fragment (with getActivity) or from Dialog/DialogFragment
*/
fun Activity.hideKeyboard() {
val imm = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
val view = currentFocus ?: View(this)
imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
Ps ,当我看到你的语言是 java 时为时已晚,我已附上参考文献,这将帮助你转换。
感谢@Anshul 的回答。我将正是我所需要的,因为后退按钮总是隐藏键盘。
在Activity
override fun onBackPressed() {
if (fragment?.isKeyBoardVisible() != true) super.onBackPressed()
}
片段
fun isKeyBoardVisible() = ViewCompat.getRootWindowInsets(requireView())?.isVisible(WindowInsetsCompat.Type.ime()) ?: true