下面的代码是第三个和第四个 Activity 的源代码。第三个 Activity 接收用户输入并对其执行数学运算。然后将原始值和处理后的值都发送到需要显示的第四个 Activity 。我不太熟悉 Intent 的使用。有人可以检查我的代码吗?它不起作用。
第三个 Activity
public class Third extends Activity {
double x=0, val1=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third);
final EditText et;
final Button b;
et = (EditText) findViewById(R.id.editText1);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Third.this, Fourth.class);
intent.putExtra("thetext", et.getText().toString());
startActivity(intent);
x=Double.parseDouble(et.getText().toString());
val1=(x*.04);
Intent in1 = new Intent(Third.this, Fourth.class);
in1.putExtra("thevalue1",val1);
startActivity(in1);
}
});
}
}
第四个 Activity :
public class Fourth extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.fourth);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(getIntent().getExtras().getString("thetext"));
TextView wt1=(TextView) findViewById(R.id.textView12);
wt1.setText(getIntent().getExtras().getDouble("thevalue1"));
}
}
最佳答案
您需要将两个附加项放入一个 Intent
中,并且只调用一次 startActivity(...)
。更改第三个 Activity
的代码(在 onClick(...)
方法中)如下...
Intent intent = new Intent(Third.this, Fourth.class);
intent.putExtra("thetext", et.getText().toString());
x=Double.parseDouble(et.getText().toString());
val1=(x*.04);
intent.putExtra("thevalue1",val1);
startActivity(intent);
关于java - 如何使用 Intent 将用户输入数据从一个 Activity 发送到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25462995/