【问题标题】:How to make a Custom View(EditText) on the actionbar automatically selected?如何自动选择操作栏上的自定义视图(EditText)?
【发布时间】:2015-07-16 21:11:23
【问题描述】:

目前我正在制作一个在操作栏上有一个 EditText 的应用程序。我通过以下链接了解了如何在操作栏上放置自定义视图: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 不同,操作栏上的编辑文本不会自动选择。有没有办法让操作栏上的这个编辑文本自动被选中?

顺便说一句,我确实查看了以下链接,How to focus and show soft keyboard when a EditText is shown in action bar? 不幸的是,我试图理解解决方案并将其应用于我的代码,但编辑文本仍未自动选择。我对堆栈溢出还是很陌生,所以如果我没有提供足够的信息/代码来解决我的问题,请在 cmets 中告诉我。谢谢!

【问题讨论】:

    标签: android xml android-actionbar android-edittext


    【解决方案1】:

    哦,所以在做了一些研究之后,我发现了一种称为请求焦点的方法。请求焦点将为editText请求焦点,但我还需要添加两行代码。这两行代码会在选择editText时弹出键盘。问题解决了!下面是我如何为操作栏上的编辑文本请求焦点。

    searchq.requestFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(searchq, InputMethodManager.SHOW_IMPLICIT);
    

    【讨论】:

      【解决方案2】:

      嗯,有另一种方法可以根据需要制作自定义视图..您不必在ActionBar 中执行此操作,您可以隐藏ActionBar 并在xml 中创建您自己的视图,因为在actionbar.. 中使用时,您可能会遇到一些限制。 希望对您有所帮助...

      【讨论】:

        猜你喜欢
        • 2012-08-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-05
        相关资源
        最近更新 更多