其他分享
首页 > 其他分享> > 如何实现浮动SearchWidget Android

如何实现浮动SearchWidget Android

作者:互联网

我正在尝试在当前的Android应用程序中实现搜索小部件,但是无法完成它并且我无法实现它.以下是我的代码

主要活动

  @Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.search_bar).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    } else if (id == R.id.search_bar) {
        //startActivity(new Intent(this, SearchResultActivity.class));
        return true;
    }

    return super.onOptionsItemSelected(item);
}

菜单项

 <item android:id="@+id/search_bar"
    android:title="Search"
    android:orderInCategory="100"
    android:icon="@android:drawable/ic_menu_search"
    app:showAsAction="always|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView"/>

SearchActivity

 public class SearchResultActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search_custom_text);
    handleIntent(getIntent());
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    handleIntent(intent);
}

private void handleIntent(Intent intent) {

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

表现

<activity android:name=".MainActivity"
        android:enabled="true">

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

        <meta-data android:name="android.app.default_searchable"
            android:value=".SearchResultActivity"/>

    </activity>

    <activity android:name=".SearchResultActivity"
        android:enabled="true"
        android:launchMode="singleTop"
        android:parentActivityName=".MainActivity">

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

        <!--<meta-data android:name="android.app.default_searchable"
            android:value=".SearchResultActivity" />-->

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

    </activity>

这就是我的想法,但我还没有尝试过.我确实在我的项目中实现了一个浮动活动.从下面的内容来看,我认为这是一个浮动的活动权利.
任何帮助,将不胜感激.

解决方法:

就我而言,我曾经这样使用过

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    menu.add("Search").setIcon(android.R.drawable.ic_menu_search).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
            if(item.getTitle().toString().equalsIgnoreCase("Search")){
        showSearchDialog();
    }
    return true;
}

private void showSearchDialog() {
    // TODO Auto-generated method stub
    dialog = new Dialog(this);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.dialog_search);
    final Window window = dialog.getWindow();
    window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
    window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    findDialogViews(dialog);
    dialog.show();
}

private void findDialogViews(final Dialog dialog) {
    // TODO Auto-generated method stub
    ImageView iv = (ImageView)dialog.findViewById(R.id.calc_clear_txt_Prise);
    lvSuggestions = (ListView)dialog.findViewById(R.id.listView1);
    lvSuggestions.setOnItemClickListener((android.widget.AdapterView.OnItemClickListener) this);
    final EditText etSearch = (EditText)dialog.findViewById(R.id.calc_txt_Prise);
    iv.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if(etSearch.getText().toString().isEmpty()){
                dialog.dismiss();
                imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
            }
            else
                etSearch.setText("");
        }
    });
    etSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            **DO YOUR STUFF**
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
        }
    });
}

SearchAdapter代码:

public class SearchAdapter extends BaseAdapter{

    private Context mContext;
    private List<SearchModel> list;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    public SearchAdapter(Context mContext, List<SearchModel> list) {
        // TODO Auto-generated constructor stub
        this.mContext = mContext;
        this.list = list;
        mRequestQueue = Volley.newRequestQueue(mContext);
        mImageLoader = new ImageLoader(mRequestQueue, new LruBitmapCache());
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        if(convertView==null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item_search   , parent, false);
        }
        NetworkImageView image = (NetworkImageView)convertView.findViewById(R.id.imageView1);
        TextView tvName = (TextView)convertView.findViewById(R.id.textView1);

        SearchModel model = list.get(position);
        image.setImageUrl(model.getImage(), mImageLoader);
        tvName.setText(model.getVName());

        return convertView;
    }
}

SearchAdapter是设置下拉列表……

dialog_search布局xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<FrameLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/calc_txt_Prise"
        android:layout_width="fill_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/white"
        android:hint="Search Turf"
        android:inputType="text"
        android:paddingLeft="20dp"
        android:singleLine="true"
        android:textColor="@color/text_color"
        android:textColorHint="@android:color/darker_gray"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/calc_clear_txt_Prise"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right|center_vertical"
        android:layout_marginRight="10dp"
        android:contentDescription="@string/action_settings"
        android:src="@android:drawable/ic_menu_close_clear_cancel" />
</FrameLayout>

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/text_color" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="@color/text_color"
    android:dividerHeight="1dp" />

<View
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@color/text_color" />

标签:android,android-search,android-searchmanager
来源: https://codeday.me/bug/20190623/1273560.html