arj*_*jun 8 android android-edittext
我尝试使用实现搜索EditText.每当在EditText请求中键入文本时,都会在onTextChanged()方法中使用键入的文本发送.当我使用显示的结果更改手机的方向时,将onTextChanged()使用相同的文本再次调用方法.如何避免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);
}
}
abr*_*low 11
我刚才遇到了这个问题.所以我将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) {
                    }
                });
            }
        });
}
Dau*_*fin -2
您需要重写 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
    }
}
在清单中添加以下行
android:configChanges= "orientation"
现在,根据回调,您可以做任何您想做的事情。
| 归档时间: | 
 | 
| 查看次数: | 1110 次 | 
| 最近记录: |