我试图模仿gmail应用程序处理搜索的方式,如下所示:
如上图所示,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;
}
menu / 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 :(得分:2)
在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...
int searchSrcText = searchView.getContext().getResources()
.getIdentifier("android:id/search_src_text", null, null);
EditText editText = (EditText) searchView.findViewById(searchSrcText);
editText.setCursorVisible(true);
searchView.setActivated(true);
searchView.setPressed(true);
}
}
});
答案 1 :(得分:1)
感谢@ Azizbekian的回答(这对我来说不起作用)我终于使用了以下代码(在一个空项目中 - 支持libs 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>
我发布这个相当全面的解决方案,希望能让它再现。
答案 2 :(得分:0)
试试这个,
false
true
答案 3 :(得分: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 进行测试。
答案 4 :(得分:0)
此代码适用于自定义工具栏
SearchView.SearchAutoComplete searchTextView = mSearchView.findViewById(R.id.search_src_text);
try {
Field mCursorDrawableRes = TextView.class.getDeclaredField("mCursorDrawableRes");
mCursorDrawableRes.setAccessible(true);
mCursorDrawableRes.set(searchTextView, 0);
//This sets the cursor resource ID to 0 or @null which will make it visible on white background
} catch (Exception e) {
e.printStackTrace();
}