有人能帮助我吗?:我有两个不同的按钮,当我按下按钮1时,我想用Information1打开DetailView。当我按下按钮2时,我想打开带有Information2的DetailView 这是我为ListView和DetailView选择的教程: http://www.raywenderlich.com/5527/getting-started-with-android-development
使用ListView它可以很好地完成,但是如何使用两个按钮完成此操作?
感谢您的帮助:)
答案 0 :(得分:0)
只需点击按钮
即可进行该活动Button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,DetailActivity.class);
i.putExtra("Detail1","Detail 1");
startActivity(i);
}
});
Button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(MainActivity.this,DetailActivity.class);
i.putExtra("Detail2","Detail 2");
startActivity(i);
}
});
答案 1 :(得分:0)
好的就是这样做:
Button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(YourActivity.this,DetailActivity.class);
i.putExtra("text","Some String for this one");
startActivity(i);
}
});
Button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(YourActivity.this,DetailActivity.class);
i.putExtra("text","Some other String for this one");
startActivity(i);
}
});
如您所见,方法putStringExtra()
表单Intent
允许您发送一些信息。因此,在DetailActivity
课程中,您可以这样理解:
Intent intent = getIntent();
String text = intent.getStringExtra("text");
//and now you have the text you sent in when you created the Activity
所以你可以让你的DetailActivity
显示出与意图的附加内容一起发送的内容。