我试图将数据从活动转移到另一个活动,但我从eclipse中得到错误:
Intent类型中的方法putExtra(String,boolean)不适用于参数(String,EditText)
请帮帮我
public class MainActivity extends Activity {
EditText text1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.submit);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text1 = (EditText)findViewById(R.id.editText1);
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("text1",text1);
//start the second Activity
startActivity(intent);
// Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_activity_actions, menu);
return true;
}
}
答案 0 :(得分:1)
我认为您正在寻找的是
intent.putExtra("text1",text1.getText().toString());
传递体重和身高之类的东西可能如下所示:
String weight = text1.getText().toString(); // where text1 is the EditText with user's weight
String height = text2.getText().toString(); // where text2 is the EditText with user's height
intent.putExtra("WEIGHT", weight);
intent.putExtra("HEIGHT", height);
答案 1 :(得分:0)
您可以在活动之间传递字符串和原始数据类型,但不能在两个活动之间传递UI组件。 http://developer.android.com/reference/android/content/Intent.html
答案 2 :(得分:0)
您可以按如下方式传递字符串
intent.putExtra("text1",text1.getText().toString());
但是你无法传递UI组件,例如EditText,....
Button button = (Button) findViewById(R.id.submit);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
text1 = (EditText)findViewById(R.id.editText1);
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("text1",text1.getText().toString());
//start the second Activity
startActivity(intent);
// Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_LONG).show();
}
});
答案 3 :(得分:0)
我知道你想做什么,你想要那个EditText的文字吗?
然后这样做..
public void onClick(View v) {
text1 = (EditText)findViewById(R.id.editText1);
String desiredtext=text1.getText();
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("text1",desiredText);
//start the second Activity
startActivity(intent);
// Toast.makeText(getApplicationContext(), "text", Toast.LENGTH_LONG).show();
}