[英]Button with if condition
I am new to Android, and i am trying to make a one button open 2 activities but is not working for me.我是 Android 新手,我试图让一个按钮打开 2 个活动,但对我不起作用。 for ex: on
Mainacitivity
, there is btn_mathematics
and btn_physics
open the same activity ( Main2acitivity
) and find btn_semester1
and btn_semester2
, each button will open 2 other activities for the semester modules.例如:在
Mainacitivity
,有btn_mathematics
和btn_physics
打开相同的活动( Main2acitivity
)并找到btn_semester1
和btn_semester2
,每个按钮将打开学期模块的 2 个其他活动。
If the user on Mainacitivity clicked on: btn_mathematics
---> btn_semester1
---> will have ModulesMAT
and if clicked on the btn_semester1 same button: btn_physics
---> btn_semester1
---> will have ModulesPHY
.如果 Mainacitivity 上的用户点击:
btn_mathematics
---> btn_semester1
---> 将有ModulesMAT
并且如果点击 btn_semester1 相同的按钮: btn_physics
---> btn_semester1
---> 将有ModulesPHY
。
MainActivity XML:主要活动 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: 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"/>
I guess that no need to add xml for ModulesMAT and ModulesPHY, its pretty similar to the the others.我想不需要为 ModulesMAT 和 ModulesPHY 添加 xml,它与其他的非常相似。
and now the java code:现在是java代码:
MainActivity.java:主活动.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: 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);
}
}
In MainActivity you can pass the id for recognizing into Main2Activity.在 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.