如何在另一个Activity中使用Integers和Strings?
由于
答案 0 :(得分:4)
直接在intent
中设置值会容易得多Intent支持 putExtra(名称,值);
Intent intent = new Intent(Search.this, SearchResults.class);
EditText txt1 = (EditText) findViewById(R.id.edittext);
EditText txt2 = (EditText) findViewById(R.id.edittext2);
intent.putExtra("name", txt1.getText().toString());
intent.putExtra("state", Integer.parseInt(txt2.getText().toString()));
startActivity(intent);
....
getIntent().getStringExtra("name");
getIntent().getIntExtra("state", 0); // default
答案 1 :(得分:3)
在活动1中:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
Button search = (Button) findViewById(R.id.btnSearch);
search.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(Search.this, SearchResults.class);
Bundle b = new Bundle();
EditText txt1 = (EditText) findViewById(R.id.edittext);
EditText txt2 = (EditText) findViewById(R.id.edittext2);
b.putString("name", txt1.getText().toString());
b.putInt("state", Integer.parseInt(txt2.getText().toString()));
//Add the set of extended data to the intent and start it
intent.putExtras(b);
startActivity(intent);
}
});
}
在接收活动中:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_results);
Bundle b = getIntent().getExtras();
int value = b.getInt("state", 0);
String name = b.getString("name");
TextView vw1 = (TextView) findViewById(R.id.txtName);
TextView vw2 = (TextView) findViewById(R.id.txtState);
vw1.setText("Name: " + name);
vw2.setText("State: " + String.valueOf(value));
}
但是下一次在发布这样一个基本问题之前搜索SO。那里有很多类似的问题。