Sar*_*ran 31 android android-layout android-fragments searchview
我在片段中使用search-view元素来实现搜索功能.
<SearchView
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="7dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginBottom="7dp"
android:background="@color/white" />
Run Code Online (Sandbox Code Playgroud)

问题是只有搜索图标是可点击的搜索栏中的其他区域是不可点击的,当我点击图标时我才能搜索.

你能帮我看看整个搜索区域是否可点击.

Zee*_*far 54
这可以通过在SearchView的OnClick上将Iconified设置为false来完成.
searchBar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchBar.setIconified(false);
}
});
Run Code Online (Sandbox Code Playgroud)
参考:Eric Lui的回答
希望它会有所帮助.
小智 12
search_bar.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
search_bar.onActionViewExpanded();
}
});
Run Code Online (Sandbox Code Playgroud)
Sil*_*los 12
诀窍并不在于使整个区域可以随意展开,而是将其展开然后隐藏键盘.
首先,您在XML中的布局很好,保持原样,在Java中,您希望拥有以下内容:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//Define your Searchview
SearchView searchView = (SearchView) findViewById(R.id.search_bar);
//Turn iconified to false:
searchView.setIconified(false);
//The above line will expand it to fit the area as well as throw up the keyboard
//To remove the keyboard, but make sure you keep the expanded version:
searchView.clearFocus();
}
Run Code Online (Sandbox Code Playgroud)
它实现的是它将键盘扩展到区域的整个长度,它聚焦于它,允许整个区域可点击,然后它移除键盘,以便它向用户看,就像他们能够点击那样字段并调出键盘.
应该完成你想要的.
-sil
什么是可点击的意思?触发搜索操作或只是使编辑区域集中?如果是第一个,您可以将图标设为clickable = false.并使整个布局可单击并实现事件监听器.
<SearchView
android:id="@+id/search_bar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:click="onClick"
android:layout_marginTop="7dp"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:layout_marginBottom="7dp"
android:background="@color/white" />
Run Code Online (Sandbox Code Playgroud)
onClick方法应该是
public void onClick(View v) {
? InputMethodManager im = ((InputMethodManager) getSystemService(INPUT_METHOD_SERVICE));
im.showSoftInput(editText, 0);
?
Run Code Online (Sandbox Code Playgroud)
将此添加到您的xml中
android:iconifiedByDefault="false",它将保持打开状态。并清除它添加
clearfocus到您的searchview对象。
对于最新版本的 android - 尝试:
app:iconifiedByDefault="false"
Run Code Online (Sandbox Code Playgroud)
例子 -
<android.support.v7.widget.SearchView
android:id="@+id/source_location_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="15dp"
android:layout_marginRight="20dp"
app:iconifiedByDefault="false"
app:queryHint="Your hint text" />
Run Code Online (Sandbox Code Playgroud)