【发布时间】:2013-02-22 13:01:35
【问题描述】:
我在一个 xml 布局中有两个 SearchView:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SearchView
android:id="@+id/my_first_custom_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</SearchView>
<SearchView
android:id="@+id/my_second_custom_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/my_first_custom_view" >
</SearchView>
</RelativeLayout>
然后我通过 setContentView() 将此布局膨胀到我的 MainActivity。然后我调用方法 setQuery() 为彼此。
在屏幕旋转之前一切正常。 当我旋转屏幕时,每个 searchView 都有文本“World”而不是“Hello”和“World”。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SearchView firstSearchView = (SearchView) findViewById(R.id.my_first_custom_view);
SearchView secondSearchView = (SearchView) findViewById(R.id.my_second_custom_view);
firstSearchView.setQuery("Hello!", false);
secondSearchView.setQuery("World", false);
}
}
有人可以解释发生了什么问题吗?
【问题讨论】:
-
这是您的搜索视图在 onCreate 中获得的唯一参考吗?我假设没有.. 如果没有,那么您可能想要发布相关代码
-
我尝试了您的代码,并且与您的行为相同......所以重现问题的所有内容都在帖子中
-
是的,它是唯一的参考。我不再使用它了。