开发者

Android EditText content multiplies when orientation changed

开发者 https://www.devze.com 2023-03-15 22:47 出处:网络
I have weird situation with EditText. Situation: set device to landscape mode enter text to EditText and press

I have weird situation with EditText. Situation:

  1. set device to landscape mode
  2. enter text to EditText and press "DONE"
  3. change orientation
  4. EditText contains doubled text that was typed

the code:

private EditText enterText;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    enterText = (EditText) findViewById(R.id.enterText);
}

If I comment the line with findViewById, everything is working properly.

Any ideas why this happens?

EDIT: I've double checked and it happens every time, not only if the line I was wri开发者_运维百科tten about is commented.. :-\

EDIT2: It happens only on first orientation change event. Every next is OK..


Every time you rotate your phone the Activity restarts, which probably calls something that causes your text to be written again somehow. This is part of the OS and a bit annoying, here is that to do.

If you in your manifest add:

android:configChanges="orientation"

to the activity node in which the rotation is done, you'll have to override the onConfigurationChanged.

@Override
public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  setContentView(R.layout.myLayout);

}

Whenever your phone rotates the overriden method will call and you can do a special initialization like ignore the input if there was an input earlier and such. Hope this gets you on the right track!

PS: You can also pipeline the configChanges with different other "values/configs". Such as keyboardHidden and many others.

They are listed on the following link

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号