帮助我了解如何使用Intent。 通过代码中的“意图”使用无法进入下一个期望的活动,如下所示
public class GetIn extends Activity {
EditText editName;
EditText editPsscd;
EditText editSport;
EditText editTeam;
Button button;
final Context context = this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_in);
button = (Button)findViewById(R.id.button1);
editName = (EditText)findViewById(R.id.editText1);
editPsscd = (EditText)findViewById(R.id.editText2);
editSport = (EditText)findViewById(R.id.editText3);
editTeam = (EditText)findViewById(R.id.editText4);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(context,Success.class);
intent.putExtra("Name",editName.getText().toString());
intent.putExtra("Passcode",editPsscd.getText().toString());
intent.putExtra("Sports",editSport.getText().toString());
intent.putExtra("Team", editTeam.getText().toString());
startActivity(intent);
}
});
}
上下文的使用是否有效? 这是“Success.java”文件
public class Success extends GetIn {
public void onCreate(Bundle savedInstanceState){
TextView textName;
TextView textSport;
TextView textTeam;
super.onCreate(savedInstanceState);
setContentView(R.layout.success);
Intent intent = getIntent();
textName=(TextView)findViewById(R.id.textView1);
textName.setText("Your name is "+intent.getStringExtra("Name").toString());
textSport=(TextView)findViewById(R.id.textView2);
textSport.setText("Your Favourite sports is "+intent.getStringExtra("Sport").toString());
textTeam=(TextView)findViewById(R.id.textView3);
textTeam.setText("Your Favourite team is "+intent.getStringExtra("Team").toString());
}
}
答案 0 :(得分:4)
你应该替换这个
Intent intent = new Intent(context,Success.class);
使用
Intent intent = new Intent(GetIn.this,Success.class);
Bundle b = new Bundle();
b.putString("Name here",editName.getText().toString());
b.putString("Passcode here",editPsscd.getText().toString());
b.putString("Sports here",editSport.getText().toString());
b.putString("Team here",editTeam.getText().toString());
intent.putExtra(b);
startActivity(intent);
将Success
个活动注册到manifest.xml
进入成功活动获取Intent
值
Bundle b = getIntent().getExtras();
String Name=b.getString("Name here");
String Passcode=b.getString("Passcode here");
String Sports=b.getString("Sports here");
String Team =b.getString("Team here");
答案 1 :(得分:0)
Intent intent = new Intent(this, DisplayMessageActivity.class);
使用Context
作为其第一个参数(this
,因为Activity
类是Context
的子类)
也改变了这个
public class Success extends GetIn
到
public class Success extends Activity