2017-03-23 36 views
1

我試圖模仿如何搜索是Gmail應用程序,它看起來處理的樣式如下:如圖所示閃爍在搜索查看文本後光標沒有表現出軟鍵盤

enter image description here

上面有一個(藍色)在searchView中的搜索查詢後閃爍光標。

我相應的代碼如下所示:

SearchReslutActivity.class

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu, menu); 

    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
    SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView(); 
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
    searchView.setQuery(getIntent().getStringExtra(SearchManager.QUERY), false); 
    searchView.setIconified(false); 
    searchView.clearFocus(); 

    return true; 
} 

菜單/ search_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

<item 
    android:id="@+id/search" 
    android:title="@string/search_title" 
    app:actionViewClass="android.support.v7.widget.SearchView" 
    app:showAsAction="always" /> </menu> 

XML/searchable.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android" 
android:hint="@string/hint" 
android:includeInGlobalSearch="true" 
android:inputType="textCapWords" 
android:label="@string/app_name" 
android:searchSuggestAuthority="com.webaddress.myproject.data.Provider" 
android:searchSuggestIntentAction="android.intent.action.VIEW" 
android:searchSuggestIntentData="content://com.webaddress.myproject.data.Provider/table" 
android:searchSuggestSelection=" ?" 
android:searchSuggestThreshold="1" /> 

上述代碼實現了與gmail應用程序中顯示內容相同的外觀,只是閃爍光標爲。如何在不請求關注searchView(這又會使軟鍵盤再次彈出)的情況​​下添加如此閃爍的光標?

謝謝!

+0

[Android Hide Hide Soft Keyboard from EditText while not lose cursor]可能重複(http://stackoverflow.com/questions/13586354/android-hide-soft-keyboard-from-edittext-while-not-losing-cursor ) –

+0

我追蹤了來源。問題來自[this](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/android/support/v7/widget/SearchView.java# 1696)行:'mSearchView.clearFocus();'。如果這個被註釋掉了,閃爍就可以工作了。 – azizbekian

+0

是的,但是當我註釋掉'mSearchView.clearFocus();'鍵盤會再次彈出... – Joni

回答

0

感謝@ Azizbekian的回答(這並沒有對自己的工作對我來說)我終於得到它用下面的代碼工作(在一個空的項目 - 支持庫25.3.0):

的java/MainActivity

package com.yourwebpage.yourapplication; 

import android.app.SearchManager; 
import android.os.Bundle; 
import android.support.v4.view.MenuItemCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.SearchView; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 

public class MainActivity extends AppCompatActivity { 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(final Menu menu) { 
     getMenuInflater().inflate(R.menu.search_menu, menu); 
     return true; 
    } 

    @Override 
    public boolean onPrepareOptionsMenu(final Menu menu) { 
     MenuItem menuItem = menu.findItem(R.id.search); 
     SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem); 
     searchView.setQuery(getIntent().getStringExtra(SearchManager.QUERY), false); 
     searchView.setIconified(false); 
     SearchView.SearchAutoComplete searchAutoComplete = 
       (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text); 
     searchAutoComplete.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(final View v, final boolean hasFocus) { 
       v.requestFocus(); 
      } 
     }); 
     searchView.clearFocus(); 
     return super.onPrepareOptionsMenu(menu); 
    } 
} 

RES /佈局/ activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.yourwebpage.yourapplication.MainActivity"> 
</android.support.constraint.ConstraintLayout> 

RES /菜單/ search_menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <item 
     android:id="@+id/search" 
     android:title="Search" 
     app:actionViewClass="android.support.v7.widget.SearchView" 
     app:showAsAction="always"/> 
</menu> 

艙單/ AndroidManifest。XML

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.yourwebpage.yourapplication"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:roundIcon="@mipmap/ic_launcher_round" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity android:name=".MainActivity" 
      android:windowSoftInputMode="stateAlwaysHidden"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

我張貼在使之可重複的,從而希望這個相當全面的解決方案。

0

試試這個,

searchView.setOnSearchClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      searchView.setImeOptions(EditorInfo.IME_ACTION_SEARCH); 
     } 
    }); 
+0

這並沒有使光標閃爍。我用更多的代碼(xml)更新了我的問題,也許有幫助。謝謝! – Joni

1

添加該代碼在onCreate()

searchView.setOnFocusChangeListener(new View.OnFocusChangeListener() { 

     @Override 
     public void onFocusChange(View v, boolean hasFocus) { 

      if (!searchView.hasFocus()) { 
       //this if condition is true when searchview lost focus... 
       final EditText e = (EditText) searchView.findViewById(searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null)); 
       e.setCursorVisible(true); 
       searchView.setActivated(true); 
       searchView.setPressed(true); 

      } 
     } 
    }); 
+1

仍然不閃爍。使用'25.3.0'中的'android.support.v7.widget.SearchView'在API 21上運行。 – azizbekian

+0

是的,我也一樣。如果我將代碼放入我的'onCreate()'我的應用程序將崩潰。如果我把它放在我的'onCreateOptionsMenu()'中,什麼都不會發生,即當我調用'searchView.clearFocus()'時,甚至沒有輸入'onFocusChange()'。我究竟做錯了什麼?我已經添加了我的各自的XML文件,希望我的錯誤現在變得更加清晰。 – Joni

0

執行此:

@Override 
public boolean onCreateOptionsMenu(final Menu menu) { 
    getMenuInflater().inflate(R.menu.search_menu, menu); 
    return true; 
} 

@Override 
public boolean onPrepareOptionsMenu(final Menu menu) { 
    MenuItem menuItem = menu.findItem(R.id.search); 
    SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem); 
    SearchView.SearchAutoComplete searchAutoComplete = 
     (SearchView.SearchAutoComplete) searchView.findViewById(R.id.search_src_text); 
    searchAutoComplete.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
     @Override 
     public void onFocusChange(final View v, final boolean hasFocus) { 
      v.requestFocus(); 
     } 
    }); 
    return super.onPrepareOptionsMenu(menu); 
} 

search_menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <item 
      android:id="@+id/search" 
      android:title="Search" 
      app:actionViewClass="android.support.v7.widget.SearchView" 
      app:showAsAction="ifRoom"/> 

</menu> 

使用支持庫進行測試版本25.3.0

Result

+0

謝謝!當我把你的代碼插入我的時候,確實有一個閃爍的光標,但是還有鍵盤再次彈出......也許我在將你的代碼片段與我現有的代碼庫合併時做錯了什麼。你可能會更新你的答案,顯示我的整個'onCreateOptionsMenu()'應該看起來像最後? – Joni

+0

@Joni,更新。 – azizbekian

+0

好的,我所有的關於'searchableInfo'(SearchManager'和'setSearchableInfo'初始化)的代碼以及我在'SearchView'中顯示搜索查詢的調用(以前由'setQuery( )')? – Joni