我是Android新手,我正试图打开一个按钮打开2个活动,但不适合我。
对于前:
在Mainacitivity
,btn_mathematics
和btn_physics
打开相同的活动(Main2acitivity
)并找到btn_semester1
和btn_semester2
,每个按钮都会打开2学期模块的其他活动。
如果用户点击了Mainacitivity,请点击:
btn_mathematics
---> btn_semester1
--->将有ModulesMAT
如果点击btn_semester1相同的按钮:
btn_physics
---> btn_semester1
--->将有ModulesPHY
。
MainActivity XML:
<Button
android:id="@+id/btn_mathematics"
android:onClick="btn_mathematics"
android:text="@string/btn_mathematics/>
<Button
android:id="@+id/btn_physics"
android:onClick="btn_physics"
android:text="@string/btn_physics"/>
Main2Activity XML:
<Button
android:id="@+id/btn_semester1"
android:onClick="btn_semester1"
android:text="@string/btn_semester1"/>
<Button
android:id="@+id/btn_semester2"
android:onClick="btn_s2"
android:text="@string/btn_semester2"/>
我想没有必要为ModulesMAT和ModulesPHY添加xml,它与其他的非常相似。
现在是java代码:
MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btn_mathematics (View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
`public void btn_physics (View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
}
Main2Activity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void btn_semester1 (View v)
{
Intent i = getIntent();
String id = i.getStringExtra("id");
if(id == "btn_mathematics")
{
i = new Intent(this, ModulesMAT.class);
startActivity(i);
}
else if (id == "btn_physics")
{
i = new Intent(this, ModulesPHY.class);
startActivity(i);
}
}
public void btn_semester2 (View v)
{
Intent i = getIntent();
String id = i.getStringExtra("id");
if(id == "btn_mathematics")
{
i = new Intent(this, ModulesMAT2.class);
startActivity(i);
}
else if (id == "btn_physics")
{
i = new Intent(this, ModulesPHY2.class);
startActivity(i);
}
}
答案 0 :(得分:1)
在MainActivity中,您可以将用于识别的ID传递给Main2Activity 的 MainActivity 强>
public void btn_mathematics (View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
Bundle bundle = new Bundle();
bundle.putString("id","Math");
intent.putExtra(bundle);
startActivity(intent);
}
public void btn_physics (View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
Bundle bundle = new Bundle();
bundle.putString("id","Physics");
intent.putExtra(bundle);
startActivity(intent);
}
<强> Main2Activity 强>
String id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Bundle bundle = getIntent().getExtras();
id= bundle.getString("id");
}
public void btn_semester1 (View v)
{
if(id == "Math")
{
i = new Intent(this, ModulesMAT2.class);
startActivity(i);
}
else if (id == "Physics")
{
i = new Intent(this, ModulesPHY2.class);
startActivity(i);
}
}
public void btn_semester2 (View v)
{
if(id == "Math")
{
i = new Intent(this, ModulesMAT2.class);
startActivity(i);
}
else if (id == "Physics")
{
i = new Intent(this, ModulesPHY2.class);
startActivity(i);
}
}