首页 > 解决方案 > 在前一个 Activity 的 RadioButton 中选择另一个项目时,当前 Activity 的文本框不会更新。安卓/Java

问题描述

应用程序应计算轮廓的惯性矩和阻力力矩,并在 TextView 中输出所选轮廓的值。申请工作:

  1. 用户输入载荷、挠度、长度的值
  2. 通过 radioButton 选择所需的配置文件
  3. 点击“执行计算”按钮
  4. 应用程序在 TextView 中读取并显示所选配置文件的值 在第一次启动时,应用程序正常工作并显示所有内容。但是,当您退出上一个活动以通过 radioButton 更改配置文件选择时,文本字段的值不会更改,并且与上一个选择保持相同。只有重新启动整个应用程序才有帮助。事实证明,要更改横截面,您每次都需要完全终止应用程序并重新输入数据。

数据输入活动1

public void shvellerOnClick(View view) {
 
        radioGroupShveller.removeAllViews();
 
        RadioButton shvellerYRadioButton = new RadioButton(this);
        RadioButton shvellerPRadioButton = new RadioButton(this);
 
        shvellerYRadioButton.setText(R.string.shveller_Y);
        shvellerPRadioButton.setText(R.string.shveller_P);
 
        radioGroupShveller.addView(shvellerYRadioButton);
        radioGroupShveller.addView(shvellerPRadioButton);
 
        shvellerPRadioButton.setOnClickListener(radioButtonClickListener);
        shvellerYRadioButton.setOnClickListener(radioButtonClickListener);
 
        shvellerYRadioButton.setId(R.id.idShvellerYRadioButton);
        shvellerPRadioButton.setId(R.id.idShvellerPRadioButton);
 
        radioGroupShvellerX2.removeAllViews();
        radioGroupDvutavr.removeAllViews();
 
        radioGroupShvellerX2.clearCheck();
        radioGroupDvutavr.clearCheck();
 
    }
 
    public void dvaShvelleraOnClick(View view) {
 
        radioGroupShvellerX2.removeAllViews();
 
        RadioButton shvellerYX2RadioButton = new RadioButton(this);
        RadioButton shvellerPX2RadioButton = new RadioButton(this);
 
        shvellerYX2RadioButton.setText(R.string.shveller_Y_x2);
        shvellerPX2RadioButton.setText(R.string.shveller_P_x2);
 
        radioGroupShvellerX2.addView(shvellerYX2RadioButton);
        radioGroupShvellerX2.addView(shvellerPX2RadioButton);
 
        shvellerYX2RadioButton.setOnClickListener(radioButtonClickListener);
        shvellerPX2RadioButton.setOnClickListener(radioButtonClickListener);
 
        shvellerYX2RadioButton.setId(R.id.idShvellerYX2RadioButton);
        shvellerPX2RadioButton.setId(R.id.idShvellerPX2RadioButton);
 
        radioGroupShveller.removeAllViews();
        radioGroupDvutavr.removeAllViews();
 
        radioGroupShvellerX2.clearCheck();
        radioGroupDvutavr.clearCheck();
    
    }
 
 
    View.OnClickListener radioButtonClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            RadioButton rb = (RadioButton)v;
            switch (rb.getId()) {
                case R.id.idShvellerYRadioButton:
                    valueSelectedGost = 0;
                    break;
                case R.id.idShvellerPRadioButton:
                    valueSelectedGost = 1;
                    break;
                case R.id.idShvellerYX2RadioButton:
                    valueSelectedGost = 10;
                    break;
                case R.id.idShvellerPX2RadioButton:
                    valueSelectedGost = 11;
                    break;
                case R.id.idDvutavBRadioButton:
                    valueSelectedGost = 20;
                    break;
                case R.id.idDvutavKRadioButton:
                    valueSelectedGost = 21;
                    break;
            }
        }
    };
 
  
    public void vypolnitRaschetOnClick(View view) {
 
        int putGost = valueSelectedGost;
 
        Intent performCalculationIntent = new Intent(StBalkaSchema1CopyActivity.this, StBalkaShema1RaschetCopyActivity.class);
 
        performCalculationIntent.putExtra("gostInt", putGost);
 
        startActivity(performCalculationIntent);
    }

Activity2 计算

        Intent intent = getIntent();
        int getGostInt = intent.getIntExtra("gostInt",0);
  
        selectedProfileImageView = (ImageView) findViewById(R.id.selectedProfileImegeView);
        selectedProfileTextView = (TextView) findViewById(R.id.selectedProfileTextView);

        infoSelectedProfileTextView = (TextView) findViewById(R.id.infoSelectionProfileTextView);
 
 
        double loadToFormula = Double.parseDouble(loadTextView.getText().toString());
        double lengthToFormula = Double.parseDouble(lengthTextView.getText().toString());
        double deflectionToFormula = Double.parseDouble(deflectionTextView.getText().toString());
 
        double resultWtr = resultMmax * 100 / (1.12*2.1);
        double resultItr = resultMmax * Math.pow(10, 5) * lengthMToFormula * Math.pow(10, 2) * deflectionToFormula / (10 * 2.1 * Math.pow(10, 6));

 
        if (getGostInt >= 0 & getGostInt <= 9){
            selectedProfileImageView.setImageResource(R.drawable.shveller);
            if (getGostInt == 0){
                ShvellerU_GOST_8240_89.addShvellerU();
                infoSelectedProfileTextView.setText(ShvellerU_GOST_8240_89.getClosestInertiaResistance(resultItr, resultWtr));
                selectedProfileTextView.setText(R.string.shveller_Y);
            } else if (getGostInt == 1){
                ShvellerP_GOST_8240_89.addShvellerP();
                infoSelectedProfileTextView.setText(ShvellerP_GOST_8240_89.getClosestInertiaResistance(resultItr, resultWtr));
                selectedProfileTextView.setText(R.string.shveller_P);
            }else {
                infoSelectedProfileTextView.setText("Профиль не выбран");
                selectedProfileTextView.setText("Профиль не выбран");
            }
 
        } else if (getGostInt >= 10 & getGostInt <= 19){
            selectedProfileImageView.setImageResource(R.drawable.dva_shvellera);
            if(getGostInt == 10){
                ShvellerUx2_GOST_8240_89.addShvellerUx2();
                infoSelectedProfileTextView.setText(ShvellerUx2_GOST_8240_89.getClosestInertiaResistance(resultItr, resultWtr));
                selectedProfileTextView.setText(R.string.shveller_Y_x2);
            } else if (getGostInt == 11){
                ShvellerPx2_GOST_8240_89.addShvellerPx2();
                infoSelectedProfileTextView.setText(ShvellerPx2_GOST_8240_89.getClosestInertiaResistance(resultItr, resultWtr));
                selectedProfileTextView.setText(R.string.shveller_P_x2);
            } else {
                infoSelectedProfileTextView.setText("Профиль не выбран");
                selectedProfileTextView.setText("Профиль не выбран");
            }
        }
  }

配置文件添加方法

public class ShvellerU_GOST_8240_89 extends Shveller {
 
    public ShvellerU_GOST_8240_89(String name, double momentSoprotivleniya, double momentInertsii, double massa) {
        super(name, momentSoprotivleniya, momentInertsii, massa);
    }
 
    public static void addShvellerU() {
        ShvellerU_GOST_8240_89 shveller5U = new ShvellerU_GOST_8240_89("5У", 9.1,22.8,4.84);
        shveller5U.putIn(shveller5U);
 
        ShvellerU_GOST_8240_89 shveller6_5U = new ShvellerU_GOST_8240_89("6,5У", 15,48.6,5.9);
        shveller6_5U.putIn(shveller6_5U);
 
        ShvellerU_GOST_8240_89 shveller8U = new ShvellerU_GOST_8240_89("8У", 22.4,89.4,7.05);
        shveller8U.putIn(shveller8U);
 
        ShvellerU_GOST_8240_89 shveller10U = new ShvellerU_GOST_8240_89("10У", 34.8,174.0,8.59);
        shveller10U.putIn(shveller10U);
 
    }
 
}

此块活动2 计算中很可能会发生错误。

if (getGostInt == 0){
                ShvellerU_GOST_8240_89.addShvellerU();     
 infoSelectedProfileTextView.setText(ShvellerU_GOST_8240_89.getClosestInertiaResistance(resultItr, resultWtr));
selectedProfileTextView.setText(R.string.shveller_Y);

I think that this is due to the fact that when objects are added through the addShvellerU () method, they remain in memory and when a different section is selected, the addDvutavrK () method cannot overwrite another. 如何解决这个问题呢?

标签: javaandroidclassandroid-activitymethods

解决方案


推荐阅读