我正在制作一个程序,其中需要使用活动之间的意图将值从一个活动传递到另一个。
所以这是我的问题如何传递和获取意图进入另一个活动。
请参阅下面的代码,使用按钮点击我想将意图传递给其他活动..
FirstActivity.java:
public class FirstActivity extends Activity {
// Initializing variables
EditText inputName;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
inputName = (EditText) findViewById(R.id.name);
Button btnNextScreen = (Button) findViewById(R.id.btnNextScreen);
//Listening to button event
btnNextScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Code to start a new Intent and to pass value
}
});
}
答案 0 :(得分:5)
这里没什么难的,您只需要使用以下代码将名称的值从FirstActivity传递给SecondActivity:
btnNextScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Starting a new Intent
Intent nextScreen = new Intent(getApplicationContext(), SecondActivity.class);
//Sending data to another Activity
nextScreen.putExtra("name", inputName.getText().toString());
startActivity(nextScreen);
}
});
}
并且在第二个活动中使用onCreate()方法中的代码:
TextView txtName = (TextView) findViewById(R.id.txtName);
Intent i = getIntent();
// Receiving the Data
String name = i.getStringExtra("name");
txtName.setText(name);
答案 1 :(得分:1)
是 Ketlyen 我同意 @klamitsuri
你只需需要传递值,就像他在代码中所示:
Intent nextScreen = new Intent(getApplicationContext(), SecondActivity.class);
//Sending data to another Activity
nextScreen.putExtra("name", inputName.getText().toString());
startActivity(nextScreen);
并简单地使用:
Intent i = getIntent();
// Receiving the Data
String name = i.getStringExtra("name");
答案 2 :(得分:0)
在你的firstActivity中
btnNextScreen.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i= new Intent(firstActivity.this,secondActivity.class);
i.putExtra("key","mystring");// key and the value
startActivity(i);
}
});
}
在你的第二个活动onCreate()
setContentView(R.layout.second);
Bundle extras= getIntent().getExtras();
if(extras!=null)
{
TextView tv= (TextView)findviewById(R.id.textView1);
String value = extras.getString("key"); // get the value based on the key
tv.setText(value);
}
答案 3 :(得分:0)
在您当前的活动中,创建一个新的意图:
Intent i = new Intent(FirstActivity.this, NewActivity.class);
i.putExtra("new_variable_name","value");
startActivity(i);
然后在新的Activity中,检索这些值:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String value = extras.getString("new_variable_name");
}
答案 4 :(得分:0)
您可以使用startActivityForResult(intent,result);
传递值或数据并获取前一个活动发送的值覆盖onActivityResult函数(int requestCode,int resultCode,Intent data);
例如:
public class MyActivity extends Activity {
...
static final int PICK_CONTACT_REQUEST = 0;
protected boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
// When the user center presses, let them pick a contact.
startActivityForResult(
new Intent(Intent.ACTION_PICK,
new Uri("content://contacts")),
PICK_CONTACT_REQUEST);
return true;
}
return false;
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == PICK_CONTACT_REQUEST) {
if (resultCode == RESULT_OK) {
// A contact was picked. Here we will just display it
// to the user.
startActivity(new Intent(Intent.ACTION_VIEW, data));
}
}
}}