Android崩溃搜索按钮

时间:2014-08-23 20:35:16

标签: android

我无法在动作栏中折叠我的搜索按钮。这是我的菜单xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto">
      <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="Ara"
          android:showAsAction="always|collapseActionView" 
          app:actionViewClass="android.support.v7.widget.SearchView"/>

</menu>

我正在主要活动中使用这些课程

import io.socket.IOAcknowledge;
import io.socket.IOCallback;
import io.socket.SocketIO;
import io.socket.SocketIOException;

import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.Hashtable;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.impact.ribony.adapter.TabsPagerAdapter;
import com.koushikdutta.async.future.Future;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

菜单创建方法:

public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

我正在使用min sdk版本14.我正在按搜索按钮而没有任何反应。为什么按钮没有折叠?

1 个答案:

答案 0 :(得分:1)

您需要实施SearchView.OnQueryTextListener,然后

@Override
public boolean onQueryTextSubmit(String query) {
    InputMethodManager imm =
            (InputMethodManager)mActivity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(searchView.getWindowToken(), 0);
    return true;
}

BTW,SearchView初始化应该在onCreateOptionsMenu

中执行
MenuItem searchItem;
SearchView searchView;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    searchItem = menu.findItem(R.id.action_search);
    searchView = (SearchView)searchItem.getActionView();
    searchView.setQueryHint(getResources().getString(R.string.{hint})); //if you want a custom hint
    searchView.setOnQueryTextListener(this);
    EditText et = (EditText)searchView.findViewById(searchView.getContext().getResources()
            .getIdentifier("android:id/search_src_text", null, null));
    et.setHintTextColor(getResources().getColor(R.color.{color})); // if you want to change hint color
    et.setFilters(new InputFilter[] { new InputFilter.LengthFilter({max_length}) }); // if you want to set max. text limit
    return true
}