1活动采取的价值观和发送给第2个活动 [英] Taking values from 1 activity and sending to 2nd Activity
问题描述
因此,对于这个我想需要2整数值在主要活动提供的2 EditTexts用户输入并添加在一起。然后,我希望Android应用程序切换到第二个活动,并在盒子的EditText显示有进入2盒的EditText从previous 2值的总和(主)活动。
我觉得我在正确的轨道上,但我不是很肯定如何获得在第二个活动的总和。我试着束和搞乱这样的,但是我还是不太明白。
的* *重新编辑,以反映一些建议。第二届活动仍然没有显示总...我想这是某种形式的转换错误。
***,我几乎可以肯定它是与第二Activity..the首先查找我的权利
**完美!这是一个转换错误。感谢所有的帮助:D
进口android.os.Bundle;
进口android.app.Activity;
进口android.content.Intent;
进口android.view.Menu;
进口android.view.View;
进口android.widget.EditText;
公共类MainActivity扩展活动
{ @覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
} @覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
//充气菜单;如果是present这增加了项目操作栏。
。getMenuInflater()膨胀(R.menu.activity_main,菜单);
返回true;
} 公共无效的OnClick(视图V)//安卓的onClick =onclick事件添加到activity_main.xml中这个按钮
{
意向意图=新意图(MainActivity.this,Summation.class);
V1的EditText =(EditText上)findViewById(R.id.editText1);
V2的EditText =(EditText上)findViewById(R.id.editText2);
INT第一=的Integer.parseInt(v1.getText()的toString());
INT秒=的Integer.parseInt(v2.getText()的toString());
字符串总和=将String.valueOf(第一+第二);
intent.putExtra(求和,总和);
startActivity(意向);
}
}
第二个活动类
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.widget.EditText;公共类求和扩展活动
{
//第二项活动 @覆盖
保护无效的onCreate(捆绑savedInstanceState)
{
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_2);
字符串总和=(getIntent()getExtras()的getString(总和));
的EditText ADDSUM =(EditText上)findViewById(R.id.editText1);
addsum.setText(总和); }
}
您不能直接从指定的EditText你的 INT
值。您需要分配像
的EditText eText1 =(EditText上)findViewById(R.id.yourfirstedittext)
和与你的第二个相同。然后得到的值
INT firstNum =的Integer.parseInt(eText1.getValue.toString())
和当然与第二相同。然后,您可以添加这两个整数
在一起,轻松地与通过他们intent.putExtra(苏木,总和)
然后。让他们在你的第二个活动
与 getIntent()
意图CI = getIntent();
最终诠释总和= ci.getIntExtra(求和,yourDefaultIntValue)
如果您传递作为 INT
你需要做这种方式并获得了字符串
价值在 INT
。但是,如果你通过为字符串
那么你的罚款
So for this I am trying to take 2 integer values the user inputs in the 2 EditTexts provided in the main activity and add them together. Then, I want the android application to switch to the 2nd Activity and in the EditText box there display the sum of the 2 values entered into the 2 EditText boxes from the previous(main) Activity.
I think I'm on the right track, but I'm not quite certain how to obtain the sum in the second activity. I've tried messing with bundles and such, but I still can't quite figure it out.
**Re-edited to reflect some suggestions. The 2nd activity still does not show the total...I'm thinking it's a conversion error of some sort.
***I'm almost positive it has something to do with the 2nd Activity..the first looks right to me
**PERFECT! It was a conversion error. Thanks all for the help :D
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void OnClick(View v) //android:onClick="OnClick" added to activity_main.xml for this button
{
Intent intent = new Intent(MainActivity.this, Summation.class);
EditText v1 = (EditText)findViewById(R.id.editText1);
EditText v2 = (EditText)findViewById(R.id.editText2);
int first = Integer.parseInt(v1.getText().toString());
int second = Integer.parseInt(v2.getText().toString());
String sum = String.valueOf(first + second);
intent.putExtra("sum", sum);
startActivity(intent);
}
}
Second Activity Class
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
public class Summation extends Activity
{
//Second Activity
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
String sum = (getIntent().getExtras().getString("sum"));
EditText addsum = (EditText)findViewById(R.id.editText1);
addsum.setText(sum);
}
}
You can't assign your int
value straight from edittext. You need to assign the edit texts like
EditText eText1 = (EditText) findViewById(R.id.yourfirstedittext)
and the same with your second. Then get the values
int firstNum = Integer.parseInt(eText1.getValue.toString())
and of course the same with the second. You can then add these two ints
together and simply pass them with intent.putExtra("sume, sum)
. Then get them in your second Activity
with getIntent()
Intent ci = getIntent();
final int sum = ci.getIntExtra("sum", yourDefaultIntValue)
If you are passing as an int
you will need to do it this way and get the String
value of the int
. But if you pass as a String
then you are fine
这篇关于1活动采取的价值观和发送给第2个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!