一个活动中使用两个SearchView以及屏幕旋转问题

7

I have two SearchViews in one xml layout:

<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中,然后为每个searchView调用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);
    }
}

有人能解释一下出了什么问题吗?

你的searchviews只在onCreate中获取引用吗?我猜不是..如果不是,那么你可能需要发布相关代码。 - JoxTraex
1
我尝试了你的代码,和你的行为一样……所以所有重现问题的东西都在这篇文章中。 - ben75
是的,这是唯一的一个引用。我不再使用它了。 - lukjar
3个回答

13

SearchView使用从充气布局文件得出的视图作为其内容。因此,所有在活动布局中使用的SearchView(如您的情况)将具有相同 id 的视图作为内容。当 Android 尝试保存状态以处理配置更改时,它将看到来自SearchViewsEditTexts具有相同的 ID,并且它将为所有这些视图恢复相同的状态。

解决此问题的最简单方法是像这样使用ActivityonSaveInstanceStateonRestoreInstanceState

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    // state for the first SearchView
    outState.putString("sv1", firstSearchView.getQuery().toString());
    // state for the second SearchView
    outState.putString("sv2", secondSearchView.getQuery().toString());
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    // properly set the state to balance Android's own restore mechanism
    firstSearchView.setQuery(savedInstanceState.getString("sv1"), false);
    secondSearchView.setQuery(savedInstanceState.getString("sv2"), false);
}

还可以看看这个相关问题


谢谢。它有效;)。在这种情况下,onRestoreInstanceState()方法中的恢复实例非常重要。当我在onCreate中执行此操作时,它不起作用。可能是因为onCreate中的“Hello”和“World”值在onRestore方法中被覆盖了。 - lukjar

-1

缓解这个问题的一种方法是在您的活动中捕获方向事件更改,然后在两个搜索视图上再次设置查询。


你能写更多关于这个的内容吗? - lukjar
这并不太复杂,可以在Google上搜索如何监听活动中的方向更改,然后重新配置您的两个搜索视图。 - JoxTraex

-2

在拥有两个SearchView的活动中,将此添加到清单中

 android:configChanges="keyboardHidden|orientation|screenSize"

@loocash1989,请检查我的答案并告诉我是否有帮助。 - baldguy
1
好的...这不是问题的答案。它有一些缺点,但是可以算是一种变通方法。 - ben75
这对我来说不是一个好的解决方案,但我承认它也可以工作。我正在寻找SearchView内部的错误。此外,我的Activity在屏幕旋转后应该重新创建,我不想改变其默认行为。感谢您的回答。我认为它可能对某些人有帮助。 - lukjar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接