我知道我们可以在活动工具栏中找到搜索小部件。根据我的要求,我们无法使用工具栏进行搜索小部件,是否可以将搜索小部件作为活动中的单独实体放在工具栏下方或某些我们想要的地方(就像文本视图或按钮一样)?
答案 0 :(得分:1)
我创建了搜索视图xml并通过包含我想要的任何地方来使用它。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:padding="16dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@android:color/white"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.SearchView
android:id="@+id/searchView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:queryHint="Search..">
</android.support.v7.widget.SearchView>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
包含在布局
中<include layout="@layout/search_view" />
答案 1 :(得分:0)
是的,有可能......这样做......
在XML
中<FrameLayout
android:focusableInTouchMode="true"
android:id="@+id/searchBoxFrame"
android:layout_below="@+id/textViewTitle"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="16dp"
android:layout_width="fill_parent">
<EditText
android:background="@drawable/input_box"
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "
android:drawableLeft="@android:drawable/ic_menu_search"
android:hint="Search"
android:id="@+id/autoText"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:lines="1"
android:paddingLeft="7dp"
android:textSize="20sp" />
<Button
android:background="@android:drawable/ic_menu_close_clear_cancel"
android:id="@+id/searchBoxCancel"
android:layout_gravity="right|center_vertical"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_width="40dp"
android:visibility="gone" />
</FrameLayout>
在Java中
private EditText searchEditText;
private Button cancelButton;
searchEditText = (EditText) findViewById(R.id.autoText);
cancelButton = (Button) findViewById(R.id.searchBoxCancel);
private void onEditTextItemChanged() {
searchEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
list.clear();
String editTextString = searchEditText.getText().toString();
int textLength = editTextString.length();
if (textLength > 0) {
cancelButton.setVisibility(View.VISIBLE);
} else {
cancelButton.setVisibility(View.GONE);
}
for (int i = 0; i < list.size(); i++) {
String searchText = "";
searchText = list.get(i).getName().toString();
if (textLength <= searchText.length()) {
// compare the String in EditText with Names in the
// ArrayList
if (searchText.toLowerCase().contains(editTextString.toLowerCase())) {
list.add(list.get(i));
}
}
}
listItemsAdapter = new listItemsAdapter(getApplicationContext(), list);
listView.setAdapter(listItemsAdapter);
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
private void onCancelButtonClicked() {
cancelButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchEditText.setText("");
}
});
}