简体   繁体   English

Android - onTextChanged()在手机方向更改时调用

[英]Android - onTextChanged() called on when phone orientation is changed

I tried to implement search using EditText . 我尝试使用EditText实现搜索。 whenever a text is typed in the EditText request is sent with the typed text in onTextChanged() method. 只要在EditText中输入文本, onTextChanged()onTextChanged()方法中使用键入的文本发送请求。 When I change the orientation of the phone with the results displyed, onTextChanged() method is called again with same text. 当我使用显示的结果更改手机的方向时,将使用相同的文本再次调用onTextChanged()方法。 How can I avoid redundant call to onTextChanged() method on orientation change. 如何避免在方向更改时对onTextChanged()方法进行冗余调用。

    public void onTextChanged(CharSequence s, int start, int before, int count) {

    final String enteredKeyword = s.toString();


    if(isFragmentVisible && !enteredKeyword.equals("")) {

    searchTimer.cancel();
    searchTimer = new Timer();
    TimerTask searchTask = new TimerTask() {
    @Override
    public void run() {
          searchUser(enteredKeyword);
    }
};
searchTimer.schedule(searchTask, 1500);
Log.i("", enteredKeyword);
}
}

I've got this problem just. 我刚才遇到了这个问题。 So I moved addTextChangedListener to the post method of EditText in the onCreateView: 所以我将addTextChangedListener移动到onCreateView中的EditText的post方法:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    ...

    EditText mSearchQuery = findViewById(R.id.search_query);
    mSearchQuery.post(new Runnable() {
            @Override
            public void run() {
                mSearchQuery.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) {
                        //Some stuff
                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                    }
                });
            }
        });
}

You need to override onConfigurationChanged method to get callback whenever orientation is getting changed. 您需要覆盖onConfigurationChanged方法,以便在方向发生变化时获得回调。

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // landscape
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        // portrait
    }
}

Add below line in manifest 在清单中添加以下行

android:configChanges= "orientation"

Now based on the callback you can do whatever you wanted to do. 现在基于回调你可以做任何你想做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 即使没有更改文本,android editText onTextChanged也会调用几十次 - android editText onTextChanged called dozens of times even when no text is changed addTextChangedListener 和 onTextChanged 在 Android Fragment 加载时总是被调用 - addTextChangedListener and onTextChanged are always called when Android Fragment loads 如果更改了手机方向,则Android中的Map后台活动将再次开始 - Background Activity for Map in Android started again if phone orientation is changed Android:方向改变时的后退动画 - Android: backbutton animation when orientation is changed 方向更改时Android中的ViewPagerIndicator问题 - Issue with ViewPagerIndicator in Android when orientation is changed 更改为横向时,Android应用程序崩溃 - Android app crashes when changed to landscape orientation 更改方向后重新加载Titanium Android - Titanium android reloading when orientation was changed 更改方向时,Android应用会关闭 - Android app closes when the orientation is changed 更改方向时可以看到/不看到Android Image - Android Image is seen/not seen when orientation is changed 更改屏幕方向后,Android应用终止 - Android app terminates when screen orientation is changed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM