方向改变时 EditTexts 不工作?

EditTexts aren't working when orientation changes?

我需要重新格式化现有的 Android 应用程序以获得横向版本。当数字输入 2 个 EditText(账单金额和小费百分比)时,它使用 TextWatcher 来计算小费金额。它自己工作,但是当我添加第二个布局时,TextWatcher 停止工作并且不计算任何东西。它旋转并看起来像我想要的那样,但不起作用。我的横向布局与具有相同 EditText 和 TextView ID 的纵向布局具有所有相同的部分。这就是我用来更改布局的内容:

package com.example.tiporientation;

import androidx.appcompat.app.AppCompatActivity;

import android.content.res.Configuration;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.TextView;
import java.text.NumberFormat;

public class MainActivity extends AppCompatActivity {

    private TipCalculator tipCalc;
    private NumberFormat money = NumberFormat.getCurrencyInstance();
    private EditText billEditText;
    private EditText tipEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        tipCalc = new TipCalculator(.17f,100);
        setContentView(R.layout.activity_main);

        billEditText = findViewById(R.id.EDIT_BillAmount);
        tipEditText = findViewById(R.id.EDIT_EnterTip);

        //create inner-class for this... puts it at bottom of MainActivity
        //TextChangeHandler is a "listener"
        //attach it to our EDIT texts, so it is listening to changes in bill $ & tip %
        TextChangeHandler tch = new TextChangeHandler();
        billEditText.addTextChangedListener(tch);
        tipEditText.addTextChangedListener(tch);
        Configuration config = getResources().getConfiguration();
        modifyLayout(config);
    }

    private void modifyLayout(Configuration newConfig) {
        if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
            setContentView(R.layout.activity_main_landscape); //we create new XML for this layout
        else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
            setContentView(R.layout.activity_main);
    }

    public void onConfigurationChanged(Configuration newConfig) {
        Log.w("MainActivity", "Inside onConfigurationChanged");
        super.onConfigurationChanged(newConfig);
        modifyLayout(newConfig); //if orientation changes again, send back to modifyLayout
    }

    public void calculate () {
        //convert edit texts to a string ???
        String billString = billEditText.getText().toString();
        String tipString = tipEditText.getText().toString();

        //2 text views from XML
        TextView tipTextView = findViewById(R.id.TXT_TipTotal);
        TextView totalTextView = findViewById(R.id.TXT_TotalAmount);

        try {
            //convert billString to float & tipString to int -- can't do math with strings
            float billAmount = Float.parseFloat(billString);
            int tipPercent = Integer.parseInt(tipString);
            //update the model -- referencing TipCalculator class!
            tipCalc.setBill(billAmount);
            tipCalc.setTip(.01f * tipPercent);
            //ask model to calculate
            float tip = tipCalc.tipAmount();
            float total = tipCalc.totalAmount();
            //update view with formatted tip & total amount
            tipTextView.setText(money.format(tip));
            totalTextView.setText(money.format(total));
        } catch(NumberFormatException nfe) { }
    }

    //create implements... select all 3 they are then stubbed out in the class
    private class TextChangeHandler implements 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) {
        }
        @Override
             public void afterTextChanged(Editable s) {
             calculate();
        }
    }
}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.tiporientation">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:configChanges="orientation|screenSize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

通过 onConfigurationChanged 回调,您的 activity 被销毁并重新创建。 文本字段中的值可以存储在 savedInstanceState 中,然后在 onCreate 中检索。

我不完全确定,但我认为这里发生的是:

  • Phone轮换
  • Phone 重新创建 activity(您的 onCreate 运行s)
  • 您收到 onConfiguration 回调
  • 你分配了一个新的 contentView(_landscape)
  • 这样你就破坏了刚刚在 onCreate 和你的监听器中呈现的布局

您的 onCreate 已经 运行,因此您的 TextWatcher 已附加到您刚刚使用来自 modifyLayout.

的 setContentView 调用破坏的布局

我仍然认为你应该让 android 为你管理这个,但为了解决这个问题,我建议:

  • 从 onCreate 中删除代码,从第一个 findViewById 开始直到(包括).addTextWatcher 行

  • 通过 onCreate

  • 中的额外 modifyLayout 调用消除其他混乱
  • 将这段代码放在一个方法中,比如 connectWatchers

  • onCreate

  • 调用此方法
  • modifyLayout

  • 调用此方法

它应该是这样的:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    tipCalc = new TipCalculator(.17f,100);
    setContentView(R.layout.activity_main);
    connectWatchers();
}

private void modifyLayout(Configuration newConfig) {
    if(newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE)
        setContentView(R.layout.activity_main_landscape); //we create new XML for this layout
    else if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT)
        setContentView(R.layout.activity_main);

    connectWatchers();
}

public void onConfigurationChanged(Configuration newConfig) {
    Log.w("MainActivity", "Inside onConfigurationChanged");
    super.onConfigurationChanged(newConfig);
    modifyLayout(newConfig); //if orientation changes again, send back to modifyLayout
}

private void connectWatchers() {
    billEditText = findViewById(R.id.EDIT_BillAmount);
    tipEditText = findViewById(R.id.EDIT_EnterTip);
    TextChangeHandler tch = new TextChangeHandler();
    billEditText.addTextChangedListener(tch);
    tipEditText.addTextChangedListener(tch);
}