gpt4 book ai didi

android - 如何在Android中实现Floating SearchWidget

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:00:39 25 4
gpt4 key购买 nike

我正在尝试在当前的 Android 应用程序中实现搜索小部件,但无法完成 Google I/O app而且我一直无法实现它。下面是我的代码

主 Activity

  @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"/>

搜索 Activity

 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);
}
}
}

list

<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>

这是我的想法,但我还没有尝试过。我确实在我的项目中实现了 float Activity 。从下面的样子,我认为这是一个 float 的 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
}
});
}

搜索适配器代码:

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" />

Rendered view

关于android - 如何在Android中实现Floating SearchWidget,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31085086/

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