gpt4 book ai didi

Android 折叠搜索按钮

转载 作者:行者123 更新时间:2023-11-30 02:43:14 26 4
gpt4 key购买 nike

我无法折叠操作栏中的搜索按钮。这是我的菜单 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>

我在主要 Activity 中使用这个类

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。我按下搜索按钮但没有任何反应。为什么按钮没有折叠?

最佳答案

你需要实现SearchView.OnQueryTextListener,然后

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

顺便说一句,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
}

关于Android 折叠搜索按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25466136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com