java - 当方向改变时,EditTexts 不起作用?

标签 java android android-edittext android-orientation

我需要重新格式化现有的 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();
        }
    }
}

list :

<?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中检索。

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

  • 手机旋转
  • 手机重新创建 Activity(您的 onCreate 运行)
  • 您收到 onConfiguration 回调
  • 您可以在其中分配一个新的 contentView(_landscape)
  • 这样你就销毁了刚刚在 onCreate 中渲染的布局和你的监听器

您的onCreate已经运行了,因此你的 TextWatcher附加到您刚刚通过 setContentView 调用从 modifyLayout 销毁的布局.

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

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

  • 通过 onCreate 中的额外修改布局调用消除其他困惑

  • 将此代码放入一个方法中,例如 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);
}

关于java - 当方向改变时,EditTexts 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60551827/

相关文章:

android - Robolectric ContentProvider 测试

android - 空 EditText 问题

java - Android - 以编程方式在 EditText 内居中文本

java - 如何建立4个项目(示例)并让它们互相访问?

android - Apache HTTP 库在 API 27 中不起作用

java - 需要 : array or java. lang.Iterable

android - 用于 Android 的 Paypal 授权和捕获 API

android - 在自定义 View 中禁用 EditText OnTouchListener 或 OnClickListener

java - 如何在单元测试中模拟 REST API?

java - Camel jetty 在 2 个端口和协议(protocol)上的休息方法