简体   繁体   English

如何从 android 中的另一个 java class 调用方法?

[英]How to call a method from another java class in android?

I have created an extra.java file named morning_evening.我创建了一个名为 Morning_evening 的 extra.java 文件。 I just wanted to call a method named morning() from morning_evening java file to the main activity.我只是想从 Morning_evening java 文件中调用一个名为 Morning() 的方法到主要活动。 In the morning() method I have declared some variables which I need to access in the mainActivity.在morning() 方法中,我声明了一些我需要在mainActivity 中访问的变量。

Here's my morning_evening java file这是我的早晚 java 文件

 public class morning_evening extends MainActivity {

    public void morning() throws ParseException {

        Date d = Calendar.getInstance().getTime();
        SimpleDateFormat tf = new SimpleDateFormat("HH:mm");
        String time = tf.format(d);
        String early_time = "04:00";
        Date early_time1 =  new SimpleDateFormat("HH:mm").parse(early_time);
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(early_time1);
        calendar1.add(Calendar.DATE,1);

        String end_time = "10:00";
        Date end_time1 = new SimpleDateFormat("HH:mm").parse(end_time);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(end_time1);
        calendar2.add(Calendar.DATE,1);

        Date Current_time = new SimpleDateFormat("HH:mm").parse(time);
        Calendar calendar3 = Calendar.getInstance();
        calendar3.setTime(Current_time);
        calendar3.add(Calendar.DATE,1);
        Date x = calendar3.getTime();
    }

}

And here is my MainAcivity file这是我的 MainAcivity 文件

      morning_evening1 m = new morning_evening1();
                        m.morning();

      if(x.after(calendar1.getTime()) && x.before(calendar2.getTime())){
           List<String> morning = Arrays.asList(
           "Good morning my dear and have a nice day",
           "Same to you my dear and have a nice day"
         );
       Random random = new Random();
       textView.setVisibility(View.VISIBLE);
       textView.setText(morning.get(random.nextInt(morning.size())));
    }else{
          textView.setVisibility(View.VISIBLE);
          textView.setText("Hello my dear, don't be silly, Its not morning, Its "+time);
     }

I want to access calendar1, calendar2 and x from the java file to the mainActivity.我想从 java 文件访问 calendar1、calendar2 和 x 到 mainActivity。 I don't know how to do it.我不知道该怎么做。 It would be really helpfull if you help me.如果您帮助我,那将非常有帮助。 Thank you谢谢

I have created an extra.java file named morning_evening.我创建了一个名为 Morning_evening 的 extra.java 文件。 I just wanted to call a method named morning() from morning_evening java file to the main activity.我只是想从 Morning_evening java 文件中调用一个名为 Morning() 的方法到主要活动。 In the morning() method I have declared some variables which I need to access in the mainActivity.在morning() 方法中,我声明了一些我需要在mainActivity 中访问的变量。

Here's my morning_evening java file这是我的早晚 java 文件

 public class morning_evening extends MainActivity {

    public void morning() throws ParseException {

        Date d = Calendar.getInstance().getTime();
        SimpleDateFormat tf = new SimpleDateFormat("HH:mm");
        String time = tf.format(d);
        String early_time = "04:00";
        Date early_time1 =  new SimpleDateFormat("HH:mm").parse(early_time);
        Calendar calendar1 = Calendar.getInstance();
        calendar1.setTime(early_time1);
        calendar1.add(Calendar.DATE,1);

        String end_time = "10:00";
        Date end_time1 = new SimpleDateFormat("HH:mm").parse(end_time);
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTime(end_time1);
        calendar2.add(Calendar.DATE,1);

        Date Current_time = new SimpleDateFormat("HH:mm").parse(time);
        Calendar calendar3 = Calendar.getInstance();
        calendar3.setTime(Current_time);
        calendar3.add(Calendar.DATE,1);
        Date x = calendar3.getTime();
    }

}

And here is my MainAcivity file这是我的 MainAcivity 文件

      morning_evening1 m = new morning_evening1();
                        m.morning();

      if(x.after(calendar1.getTime()) && x.before(calendar2.getTime())){
           List<String> morning = Arrays.asList(
           "Good morning my dear and have a nice day",
           "Same to you my dear and have a nice day"
         );
       Random random = new Random();
       textView.setVisibility(View.VISIBLE);
       textView.setText(morning.get(random.nextInt(morning.size())));
    }else{
          textView.setVisibility(View.VISIBLE);
          textView.setText("Hello my dear, don't be silly, Its not morning, Its "+time);
     }

I want to access calendar1, calendar2 and x from the java file to the mainActivity.我想从 java 文件访问 calendar1、calendar2 和 x 到 mainActivity。 I don't know how to do it.我不知道该怎么做。 It would be really helpfull if you help me.如果您帮助我,那将非常有帮助。 Thank you谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM