两个SearchViews在一个活动和屏幕旋转 [英] Two SearchViews in one Activity and screen rotation

查看:119
本文介绍了两个SearchViews在一个活动和屏幕旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML布局2 SearchViews:

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()。然后,我调用方法 setQuery()的一对。

And I inflate this layout to my MainActivity by setContentView(). Then I call methods setQuery() for each other.

一切正常,直到屏幕旋转。 当我轮换一次搜索查看有文字世界,而不是你好和世界的画面。

Everything is ok until a screen rotation. When I rotate the screen every searchView has text "World" instead "Hello" and "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);
    }
}

有人能解释这是怎么回事了?

Someone can explain what's going wrong ?

推荐答案

搜索查看用作其内容上看是由于膨胀布局文件。这样一来,所有的 SearchViews 的活动(如您的情况)的布局中使用会产生如内容,具有相同的IDS的看法。当Android将尝试保存状态来处理配置更改,将看到 EditTexts SearchViews 有相同的ID,并恢复同一个状态,所有的人。

The SearchView uses as its content the view resulted from inflating a layout file. As a result, all the SearchViews used in the layout of an activity(like your case) will have as content, views with the same ids. When Android will try to save the state to handle the configuration change it will see that the EditTexts from the SearchViews have the same id and it will restore the same state for all of them.

要解决这个问题,最简单的方法是使用活动的onSaveInstanceState onRestoreInstanceState 是这样的:

The simplest way to handle this issue is to use the Activity's onSaveInstanceState and onRestoreInstanceState like this:

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

也看看这个<一href="http://stackoverflow.com/questions/3542333/how-to-$p$pvent-custom-views-from-losing-state-across-screen-orientation-changes">related问题。

这篇关于两个SearchViews在一个活动和屏幕旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆