我正在开发一个 android 应用程序,其中根据用户的输入进行 5 次计算......并且这 5 个计算结果必须通过 textviews 在另一个活动中显示......我做了一半代码......其中一个结果被显示在 textview 的第二个活动中,但我对如何通过其他 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;
}
}
第二次活动
结果.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"));
}