gpt4 book ai didi

java - 如何在静态参数化方法中调用 Intent 并启动新 Activity

转载 作者:行者123 更新时间:2023-11-29 18:45:39 25 4
gpt4 key购买 nike

我想在静态参数化方法中调用 Intent 并想从那里开始新 Activity 。我在我的 MainActivity 中使用名为 zodiacSign 的 mathod 调用,它有两个参数,它有效并调用另一个 Activity HoroscopeFinder。在其工作期间,我想打开一个新的 Activity 表单 HoroscopeFinder。如果有人知道请帮助。我的代码如下:

MainActivity 代码

 dateSetListener=new DatePickerDialog.OnDateSetListener(){
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

//code
month=month+1;
String date= dayOfMonth+"/"+month+"/"+year;
datePicker.setText(date);
//from here we can get day and month
String getDay= String.valueOf(dayOfMonth);
getDayInt=Integer.parseInt(getDay);
String getMonth= String.valueOf(month);
getMonthInt=Integer.parseInt(getMonth);
//method call for deduct sign using DOB
zodiacSign(getDayInt, getMonthInt);}

HoroscopeFinder 类代码

public class HoroscopeFinder {

static String astroSign="";

public static void zodiacSign(int day, int month)
{
//Toast.makeText(, "here done", Toast.LENGTH_LONG).show();
//Log.i("check", "done here...!");


if ((month == 12 && day >= 22 && day <= 31) || (month == 1 && day >= 1 && day <= 19)) {
astroSign="Capricorn";
}
else if ((month == 1 && day >= 20 && day <= 31) || (month == 2 && day >= 1 && day <= 17)) {
astroSign="Aquarius";
//astro_sign="Aquarius";
}
else if ((month == 2 && day >= 18 && day <= 29) || (month == 3 && day >= 1 && day <= 19)) {
astroSign="Pisces";
}
else ((month == 3 && day >= 20 && day <= 31) || (month == 4 && day >= 1 && day <= 19)) {
astroSign="Aries";
Intent intent=new Intent(this, AriesActivity.class);
startActivity(intent);
}

最佳答案

将您的 zodiacSign 方法更新为

   ........
........
public static void zodiacSign(int day, int month, Context c)
{
if (...) {
....
...

Intent intent=new Intent(c, AriesActivity.class);
c.startActivity(intent);
}
}

然后在 MainActivity 中将 context 传递给您的 zodiacSign 方法作为

... zodiacSign(getDayInt, getMonthInt, MainActivity.this); // this context will help you there to start a new activity

关于java - 如何在静态参数化方法中调用 Intent 并启动新 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52046598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com