我有两个Activity activity1和activity2,activity1有两个按钮,button1和button2。当点击button1它应链接到activity2应该在Action-bar中有标题" am button1"当点击button2时,它再次链接到activity2,它应该在Action-bar中有标题" am button2"。
任何人都可以帮我这样做。
答案 0 :(得分:5)
活动1类
public class ActivityOne extends Activity{
Button btnOne, btnTwo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
btnOne = (Button) findViewById(R.id.btnOne);
btnOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
intent.putExtra("title", "am Button1");
startActivity(intent);
}
});
btnTwo = (Button) findViewById(R.id.btnTwo);
btnOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(ActivityOne.this, ActivityTwo.class);
intent.putExtra("title", "am Button2");
startActivity(intent);
}
});
}
}
ActivityTwo类
public class ActivityTwo extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
String title = intent.getStringExtra("title");
getActionBar().setTitle(title);
}
}
答案 1 :(得分:2)
这是您的解决方案
活动1:点击任意按钮1
Intent intent=new Intent(this,ActivityTwo.class);
intent.putExtra("title", "M button 1");
startActivity(intent);
活动1:点击任意按钮1
Intent intent=new Intent(this,ActivityTwo.class);
intent.putExtra("title", "M button 2");
startActivity(intent);
现在开始活动2:
String title=getIntent().getStringExtra("title");
getActionBar().setTitle(title);
或
String title=getIntent().getStringExtra("title");
getSupportActionBar().setTitle(title);
答案 2 :(得分:0)
在第一项活动中
Intent mIntent;
@Override
public void onClick(View v) {
mIntent = new Intent(FirstActivity.this,SecondActivity.class);
switch (v.getId()) {
case R.id.first_btn:
mIntent.putExtra("buttonClicked", "Am Button One");
break;
case R.id.second_btn:
mIntent.putExtra("buttonClicked", "Am Button Second ");
break;}
startActivity(mIntent);}
//and in the second activity write:
private String mSelectedButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
mSelectedButton = getIntent().getIntExtra("buttonClicked", "");
}
然后使用变量mSelectedButton来设置标题
答案 3 :(得分:0)
在清单文件中声明您的第二个活动时,请将您的所需名称设置为标签:
<activity
android:name="your second activity"
android:label="@string/your 2nd activity name" >
</activity>