android - 以编程方式自定义 SearchView

标签 android android-edittext ime searchview

我想在我的应用程序中自定义搜索 View 小部件的“edittext”。我想以编程方式设置其背景可绘制对象。我还想以编程方式添加标志 android:imeOptions="flagNoExtractUi" 以便在横向模式下,软键盘只会覆盖屏幕的一半。

有人知道如何在搜索 View “edittext”上进行自定义吗?

The search view

最佳答案

我就是这样做的。我研究了操作栏 sherlock 库中的 abs_search_view.xml 并了解到 R.id.search_plate 代表一个 LinearLayour。 LinearLayout 的第一个子项是一个 View ,其类是 com.actionbarsherlock.widget.SearchView$SearchAutoComplete。所以我检索了第一个 child 并将其转换为 com.actionbarsherlock.widget.SearchView.SearchAutoComplete,进行了空检查,然后使用 searchText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI)< 设置了 IME 选项 正如 Ahmend 在评论中所建议的那样。希望这对某人有帮助。

public static void styleSearchView(SearchView searchView, Context context) {
    LinearLayout searchPlate = (LinearLayout) searchView
            .findViewById(R.id.abs__search_plate);
    // TODO
    // searchPlate.setBackgroundResource(R.drawable.your_custom_drawable);
    if (searchPlate != null)
        ((com.actionbarsherlock.widget.SearchView.SearchAutoComplete) searchPlate
                .getChildAt(0))
                .setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
    AutoCompleteTextView searchText = (AutoCompleteTextView) searchView
            .findViewById(R.id.abs__search_src_text);
    if (searchText != null)
        searchText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
    // TODO
    // searchText.setHintTextColor(context.getResources().getColor(R.color.your_custom_color));
}

enter image description here

关于android - 以编程方式自定义 SearchView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19357842/

相关文章:

android - Android 上的分阶段发布和 Beta 测试人员

android - Jsoup 重定向到起始页

android - ffmpeg 失去质量的视频

android - 如何保存用户输入并在 ListView 中检索?

android - 使用 Intent 将事件插入日历

android - 设置焦点 EditText android

java - Android Material 重新着色 EditText 下划线

java - 如何在android中将scrollview添加到keyboardView

android - 如何在移植 Android 时设置默认 IME?

android - 如何在选择输入法对话框中添加自己的自定义 IME