java - 两个 TextView 导致一个 Activity

标签 java android android-intent android-activity

我正在开发一个 android 应用程序,其中根据用户的输入进行了 5 次计算......这 5 次计算结果必须通过另一个 Activity 中的 TextView 显示......我做了一半代码......其中一个结果由 textview 在第二个 Activity 中显示,但我对如何通过其他 textview 进行其他 4 次计算感到困惑...

public void sum(View v)
    {
        long sum1=0;
        EditText et1 = (EditText) findViewById (R.id.editText1);
        EditText et2 = (EditText) findViewById (R.id.editText2);
        EditText et3 = (EditText) findViewById (R.id.editText3);
        EditText et4 = (EditText) findViewById (R.id.editText4);



        sum1=getSum(et1.getText().toString() + et2.getText().toString() + et3.getText().toString() + et4.getText().toString());

startActivity(new Intent(this, result.class).putExtra("name", sum1 + "")); 

    }

public long getSum(String text) 
    {
        // TODO Auto-generated method stub
        long sum1 = 0;
        char[] name = new char[text.length()];
        name = text.toCharArray();


        for(int i=0; i<text.length(); i++)
        {
            sum1 += value( name[i] );

        }
        while (sum1>9)
        {
            sum1 = findDigitSum(sum1);
        }

        return sum1;
    }





    public long findDigitSum(long n) 
    {
        int sum1 = 0;
        while (n != 0) 
        {
            sum1 += n % 10;
            n = n / 10;
        }
        return sum1;
    }



    private int value(char a) {
        // TODO Auto-generated method stub
        switch(a) {
        case 'A': 
           return 1;
        case 'B':
           return 2;
        case 'C':
           return 3;
        case 'D': 
            return 4;
         case 'E':
            return 5;
         case 'F':
            return 6; 
         case 'G': 
             return 7;
          case 'H':
             return 8;
          case 'I':
             return 9;
          case 'J': 
              return 1;
          case 'K':
              return 2;
           case 'L':
              return 3;
           case 'M': 
               return 4;
            case 'N':
               return 5;
            case 'O':
               return 6; 
            case 'P': 
                return 7;
             case 'Q':
                return 8;
             case 'R':
                return 9;
             case 'S': 
                 return 1;
             case 'T':
                 return 2;
              case 'U':
                 return 3;
              case 'V': 
                  return 4;
               case 'W':
                  return 5;
               case 'X':
                  return 6; 
               case 'Y': 
                   return 7;
                case 'Z':
                   return 8;
                 default:
            return 0;
    }
    }

第二个 Activity

结果.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result_xm);
    TextView txt2 = (TextView) findViewById(R.id.textView21);
    txt2.setText(getIntent().getStringExtra("name"));
   }

最佳答案

我对你在做什么感到困惑,但你可以一次传递多个 extra。如果可能,请在 sum(View v) 中进行所有计算,然后将它们全部传递。因此,将您的Intent` 更改为类似

的内容
Intent i = new Intent(this, result.class)
i.putExtra("sum1", sum1 + "");
i.putExtra("sum2". String.valueOf(sum2));
// etc
startActivity(i); 

然后在你的下一个 Activity 中使用类似

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result_xm);
    Intent i = getIntent();
    String sum1 = i.getStringExtra("sum1");
    String sum2 = i.getStringExtra("sum2");
    // etc

关于java - 两个 TextView 导致一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18474543/

相关文章:

java - 如何在我的 Gradle 项目中使用 slf4j 在 IntelliJ 中获取日志记录输出?

java - Hive NVL 不适用于列的日期类型 - NullpointerException

java - 如何以固定速率从 Android 运动传感器记录数据

android - 未针对 NFC Intent 显示正确的操作

作为额外传递的 Android 应用程序 : Runtime error when I try to get the Parcelable,

java - 创建 Artifact 别名而不复制完整的项目模块

java - 如何在 spring java 配置中设置日志记录属性?

android - 如何在 TextView 中设置默认文本以在绑定(bind)时在布局预览中看到它?

Android 选项卡将 Intent 附加信息传递给新选项卡 Activity

Android:从preferences.xml 启动Activity