如何自动选择操作栏上的自定义视图(EditText)?
How to make a Custom View(EditText) on the actionbar automatically selected?
目前我正在制作一个在操作栏上有一个 EditText 的应用程序。我从以下 link:How to set a custom view in actionbar in android? 中找到了如何在操作栏上放置自定义视图。
我使用 getSupportActionBar 将 EditText 加载到操作栏上。
getSupportActionBar.setCustomView(R.layout.actionbar_view);
它加载一个名为 actionbar_view.xml 的 xml 文件。 xml 文件的代码如下。
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/searchfield"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Search for some app" >
</EditText>
问题在于,与放置在操作栏下方区域的所有普通 EditText 不同,不会自动选择操作栏上的编辑文本。有没有办法让操作栏上的这个编辑文本自动被选中?
顺便说一句,我确实看过以下内容 link,How to focus and show soft keyboard when a EditText is shown in action bar? 不幸的是,我试图理解解决方案并将其应用于我的代码,但仍然没有自动选择编辑文本。我对堆栈溢出还是很陌生,所以如果我没有提供足够的 information/code 来解决我的问题,请在评论中告诉我。谢谢!
哦,经过一番研究,我发现了一种叫做请求焦点的方法。 Request Focus 将为 editText 请求焦点,但我还需要添加两行代码。这两行代码会在选中editText时弹出键盘。问题解决了!下面是我如何在操作栏上请求焦点以编辑文本。
searchq.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(searchq, InputMethodManager.SHOW_IMPLICIT);
好吧,还有一种方法可以根据需要创建自定义视图。您不必在 ActionBar
中执行此操作,您可以隐藏 ActionBar
并创建自己的视图在 xml
中,因为在 actionbar
中使用时可能会遇到一些限制。
希望对您有所帮助...
目前我正在制作一个在操作栏上有一个 EditText 的应用程序。我从以下 link:How to set a custom view in actionbar in android? 中找到了如何在操作栏上放置自定义视图。 我使用 getSupportActionBar 将 EditText 加载到操作栏上。
getSupportActionBar.setCustomView(R.layout.actionbar_view);
它加载一个名为 actionbar_view.xml 的 xml 文件。 xml 文件的代码如下。
<?xml version="1.0" encoding="utf-8"?>
<EditText xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/searchfield"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Search for some app" >
</EditText>
问题在于,与放置在操作栏下方区域的所有普通 EditText 不同,不会自动选择操作栏上的编辑文本。有没有办法让操作栏上的这个编辑文本自动被选中?
顺便说一句,我确实看过以下内容 link,How to focus and show soft keyboard when a EditText is shown in action bar? 不幸的是,我试图理解解决方案并将其应用于我的代码,但仍然没有自动选择编辑文本。我对堆栈溢出还是很陌生,所以如果我没有提供足够的 information/code 来解决我的问题,请在评论中告诉我。谢谢!
哦,经过一番研究,我发现了一种叫做请求焦点的方法。 Request Focus 将为 editText 请求焦点,但我还需要添加两行代码。这两行代码会在选中editText时弹出键盘。问题解决了!下面是我如何在操作栏上请求焦点以编辑文本。
searchq.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(searchq, InputMethodManager.SHOW_IMPLICIT);
好吧,还有一种方法可以根据需要创建自定义视图。您不必在 ActionBar
中执行此操作,您可以隐藏 ActionBar
并创建自己的视图在 xml
中,因为在 actionbar
中使用时可能会遇到一些限制。
希望对您有所帮助...