如何在自动选择的操作栏上创建自定义视图(EditText)? [英] How to make a Custom View(EditText) on the actionbar automatically selected?

查看:81
本文介绍了如何在自动选择的操作栏上创建自定义视图(EditText)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在制作一个在操作栏上具有EditText的应用程序.我从以下链接中找到了如何在操作栏上放置自定义视图:

Currently I am making an app that has an EditText on the action bar. I found out how to put a custom view on an actionbar from the following link: How to set a custom view in actionbar in android?. I used the getSupportActionBar to load an EditText onto the action bar.

getSupportActionBar.setCustomView(R.layout.actionbar_view);

它将加载一个名为actionbar_view.xml的xml文件.该xml文件的代码如下.

It loads an xml file called actionbar_view.xml. The code for that xml file follows below.

    <?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不同,该动作栏上的编辑文本不会自动选择.有没有一种方法可以自动选择操作栏上的编辑文本"?

The problem is that unlike all normal EditTexts placed on the area below an actionbar the edit text on the actionbar is not automatically selected. Is there a way to make this Edit Text on the action bar automatically selected?

顺便说一句,我确实看过以下链接,

By the way, I did take a look at the following link, How to focus and show soft keyboard when a EditText is shown in action bar? Unfortunately I tried to understand the solution and apply to my code but the edit text was still not automatically selected. I am still pretty new to stack overflow, so in case I am not providing enough information/code to solve my problem please tell me in the comments. Thank you!

推荐答案

哦,因此,在进行了一些研究之后,我发现了一种称为请求焦点的方法. Request Focus将请求editText的焦点,但是我还需要添加两行代码.选择EDITTEXT当这两行代码会弹出键盘.问题解决了!以下是我要求重点放在操作栏上的编辑文本的方式.

Oh, so after doing some research I found a method called request focus. Request Focus will request focus for the editText, BUT I also needed to add two more lines of code. These two lines of code will pop up the keyboard when the editText is selected. Problem solved! Below is how I requested focus for an edit text on an actionbar.

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

这篇关于如何在自动选择的操作栏上创建自定义视图(EditText)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆