1

I have a line counter for the user in my notepad app. However, when the app is in landscape vs portrait, the amount of text in the EditText takes up less space, thus, less lines; so the line count has to update when rotated.

Here is what I did to solve that problem:

I added the following method to my class, and I added this to my activity in my manifest: android:configChanges="orientation|screenSize"

I've tested it and it is going through the method when the screen is rotated.

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

    TextView linesDisplay = (TextView)findViewById(R.id.linesDisplay);
    EditText editText = (EditText)findViewById(R.id.editText);

    editText.setText("" + editText.getText().toString()); // this was just a test to see if the Edit Text needed to be "internally constructed"
    linesDisplay.setText("Lines: " + editText.getLineCount());

}

I wrote 5 lines in portrait, and it displayed "Lines: 5" just fine. Then, when I rotated to landscape, it stayed at "Lines: 5" (when it was actually 4 lines on landscape). Rotated again back to portrait, now it went to "Lines: 4". Again, back to landscape, it went to "Lines: 5".

It seems that every time I rotate, it displays the number of lines in the orientation before it. That is, it will display portrait # of lines when rotated to landscape, then landscapes # of lines when rotated back to portrait, and so on.

P --> "Lines: 5" (correct)
L --> "Lines: 5" (should be 4)
P --> "Lines: 4" (should be 5)
L --> "Lines: 5" (should be 4)
...

^ it works the same vice versa, like if I start in landscape and keep rotating back it forth, it will be one behind (after the first rotation), just like the situation above.

Any help? I assume it's because it's running through my code before it actually rotates, so the line count is behind what it should be. Is there any way to run my code after it's sure that the screen is fully rotated?

4

1 回答 1

3

把它放在你的onCreate方法中:

final View view = findViewById(R.id.idOfView);
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
    @Override
    public void onGlobalLayout() 
    {
        TextView linesDisplay = (TextView)findViewById(R.id.linesDisplay);
        EditText editText = (EditText)findViewById(R.id.editText);
        linesDisplay.setText("Lines: " + editText.getLineCount());
    }
});

在你的XML

在您的父布局属性中,添加以下内容:android:id="@+id/idOfView"

例如:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/idOfView"
    android:windowSoftInputMode="adjustResize"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
        <!-- widgets -->
    </RelativeLayout>
</ScrollView>

或这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/idOfView"
    android:windowSoftInputMode="adjustResize"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <!-- widgets -->
</LinearLayout>

部分参考

于 2013-11-03T22:13:23.590 回答