我想在静态参数化方法中调用Intent,并希望从那里开始新的活动。我在我的 MainActivity 中使用了一个名为 zodiacSign 的mathod调用,该调用带有两个参数,它可以正常工作,并调用另一个活动 HoroscopeFinder 。在工作期间,我想打开一个新的活动表格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);
}
答案 0 :(得分:1)
您可以将Activity
传递给zodiacSign
,以便它可以构造和启动意图:
public static void zodiacSign(int day, int month, Activity activity)
{
if (...) {
...
Intent intent=new Intent(activity, AriesActivity.class);
activity.startActivity(intent);
}
}
然后,回到您的MainActivity
类:
...
zodiacSign(getDayInt, getMonthInt, MainActivity.this);
答案 1 :(得分:1)
将您的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