如何在android中展开时显示searchview关闭图标?

时间:2014-12-30 13:15:15

标签: android android-imageview searchview android-search

我有一个imageView。当我点击图像时,我正在隐藏图像并启用扩展的searchview。但问题是关闭图标不可见。 如何在展开形式的searchview中显示关闭图标?

这是我的代码:

  public class HomeActivity extends Activity{
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);   
 search = (SearchView) findViewById(R.id.search);
    searchImage=(ImageView) findViewById(R.id.search_image);

  searchImage.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            search.setVisibility(View.VISIBLE);
            search.setIconifiedByDefault(false); // here i am disabling default icon
            searchImage.setVisibility(View.GONE);

        }
    });

 //This function not works as close icon not visible.
     search.setOnCloseListener(new OnCloseListener() {

        @Override
        public boolean onClose() {
            searchImage.setVisibility(View.VISIBLE);
            return false;
        }
    });

这是截图,右上方的关闭图标不可见: enter image description here

6 个答案:

答案 0 :(得分:0)

使用app:closeIcon="@drawable/ic_my_close_icon"SearchView中的android.support.v7.widget.SearchView属性

使用xmlns:app="http://schemas.android.com/apk/res-auto"

答案 1 :(得分:0)

试试这个

  searchView.setOnCloseListener(new OnCloseListener() {

        @Override
        public boolean onClose() {
            searchView.clearFocus();
            return false;
        }
    });

答案 2 :(得分:0)

当您使用[关于SearchView的完整概念]

时,将显示后退选项
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="blackmonk.mobile.activity.SearchActivity" >

<item
    android:id="@+id/action_searchh"
    android:actionViewClass="android.support.v7.widget.SearchView"
    android:icon="@drawable/ic_search"
    android:showAsAction="ifRoom|collapseActionView"
    android:title="@string/search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always"/>

</menu>

并使用

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.search_menu, menu);
    MenuItem searchItem = menu.findItem(R.id.action_searchh);
    mSearchView = (SearchView) searchItem.getActionView();
    // Associate searchable configuration with the SearchView
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.action_searchh)
            .getActionView();
    searchView.setSearchableInfo(searchManager
            .getSearchableInfo(getComponentName()));
    searchView.setQueryHint("Search here");
    //to expand search bar
    searchItem.expandActionView();
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case R.id.action_search:

        break;
    case android.R.id.home:
        onBackPressed();
        return true;
    default:
        break;
    }
    return super.onOptionsItemSelected(item);
}

如果要获取查询字符串,请使用:

Intent intent = getIntent();
    if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
        String searchQuery = intent.getStringExtra(SearchManager.QUERY);
}

不要忘记在Manifest Activity

中添加此代码
  <activity
        android:name="blackmonk.mobile.activity.SearchActivity"
        android:label="@string/app_name"
        android:screenOrientation="portrait" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </action>
        </intent-filter>

        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/search" />
    </activity>

答案 3 :(得分:0)

<div class="form-group">
                    <label>Name</label>
                    <input type="text" class="form-control" 
                    [(ngModel)]="model.name" > {{model.name}} 
                </div>
                <div class="form-group">
                        <label>Quantity</label>
                        <select class="form-control" [(ngModel)]="model.quantity">
                          <option value="1">1</option>
                          <option value="2">2</option>
                          <option value="3">3</option>
                        </select> {{model.quantity}} 
                </div>

答案 4 :(得分:0)

int searchCloseIconButtonId = SearchView.getContext().getResources()
            .getIdentifier("android:id/search_close_btn", null, null);
    ImageView closeButton = (ImageView) this.mSearchView.findViewById(searchCloseIconButtonId);
    closeButton.setImageResource(R.mipmap.close); //any icon image

如果您愿意,可以在closeButton

上添加点击监听器

答案 5 :(得分:0)

由于SearchView总是在onFocusChange之后始终将“关闭”按钮设置为Gone,所以我这样做的方式不雅。

public void onFocusChange(View view, boolean b) {
    Util.Android.scheduleRunOnUi(Util.Android.getActivity(view), ()->mCloseView.setVisibility(VISIBLE), 500);
}