简体   繁体   English

在方法之外使用方法级变量

[英]Using method-level Variables outside of the method

I am trying to use variables that have been declared inside an OnClick method of a date picker.我正在尝试使用在日期选择器的OnClick方法中声明的变量。 I understand that because these variables have been declared at method-level that they can only be used inside the method.我知道因为这些变量是在方法级别声明的,所以它们只能在方法内部使用。

However, I need to use the variables in my class in order to perform a calculation with them (which is outside of the method).但是,我需要使用我的类中的变量来对它们进行计算(这是在方法之外)。 Is there a way to "re-declare" these variables outside of the method so they can be used?有没有办法在方法之外“重新声明”这些变量,以便它们可以使用?

The variables that I need to re-use are:我需要重用的变量是:

  • int year整年
  • int monthOfYear整月
  • int dayOfMonth int dayOfMonth

Here is the code that I have:这是我拥有的代码:

 tv.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View view) {
        DatePickerDialog datePickerDialog = new DatePickerDialog(CreateLine.this, new DatePickerDialog.OnDateSetListener() {

           @Override
           public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
             monthOfYear = monthOfYear + 1;
             String Sd = String.valueOf(dayOfMonth + "/" + monthOfYear + "/" + year);
             tv.setText(Sd);
           }  

As you can see I am trying to get the date that the user has entered so that I can use that inside a calculation on down the Class.如您所见,我正在尝试获取用户输入的日期,以便我可以在类的计算中使用该日期。

Thanks谢谢

Just pass the values to a class-level method, eg:只需将值传递给类级方法,例如:

methodA() {
    tv.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            DatePickerDialog datePickerDialog = new DatePickerDialog(CreateLine.this, new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
                    monthOfYear = monthOfYear + 1;
                    String Sd = String.valueOf(dayOfMonth + "/" + monthOfYear + "/" + year);
                    tv.setText(Sd);
                    methodB(year, monthOfYear, dayOfMonth);
                }  
            }
        }
    }
}

methodB(int year, int month, int day) {
    //use values
}

If you want to modify the parameter values from the onDateSet() method (like adding 1 to monthOfYear ), and then use them to set a textview (as I see you doing in the code snippet), you can do it like this:如果您想修改onDateSet()方法中的参数值(例如将 1 添加到monthOfYear ),然后使用它们来设置monthOfYear (正如我在代码片段中看到的那样),您可以这样做:

private void setListener() {
     tv.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                DatePickerDialog datePickerDialog = new DatePickerDialog(CreateLine.this, new DatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {

                        int updatedMonthOfYear = monthOfYear + 1;
                        String sd = dayOfMonth + "/" + updatedMonthOfYear + "/" + year;
                        tv.setText(sd);
                    } 
                }
            }
     }
}

If you want to access the parameters of the onDateSet() method outside of it, because you want to use a calculation method you have written, you can pass the parameters of the onDateSet() method to that performCalculation() method, and set the returned String to the textview, like so:如果要在它之外访问onDateSet()方法的参数,因为要使用自己写的一个计算方法,可以将onDateSet()方法的参数传递给那个performCalculation()方法,并设置将字符串返回给文本视图,如下所示:

private void setListener() {
         tv.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    DatePickerDialog datePickerDialog = new DatePickerDialog(CreateLine.this, new DatePickerDialog.OnDateSetListener() {

                        @Override
                        public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {

                            yearParam = year;
                            dayOfMonthParam = dayOfMonth
                            monthParam = monthOfYear + 1;

                            String Sd = performCalculation(year, monthOfYear + 1, dayOfMonth);
                            tv.setText(Sd);
                        } 
                    }
                }
         }
}

private String performCalculation(int year, int month, day) {
    String sd;
    // perform some calculation using the parameters...
    return sd;
}

You can't access the anonymous class method scope variables outside the method.您无法访问方法之外的匿名类方法范围变量。 But you can use the same variable name with the class scope.但是您可以在类范围内使用相同的变量名。 Something like this:像这样的东西:

private int year, monthOfYear, dayOfMonth;

private void yourMethod() {
  ...
  tv.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
          DatePickerDialog datePickerDialog = new DatePickerDialog(CreateLine.this, new DatePickerDialog.OnDateSetListener() {

             @Override
             public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
               monthOfYear = monthOfYear + 1;
               String Sd = String.valueOf(dayOfMonth + "/" + monthOfYear + "/" + year);
               tv.setText(Sd);
             }
}

Then you need to set the variables value in your method:然后你需要在你的方法中设置变量值:

@Override
public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {

  // Set the variables value here:
  YourClass.this.year = year;
  YourClass.this.monthOfYear = monthOfYear;
  YourClass.this.dayOfMonth = dayOfMonth;

  monthOfYear = monthOfYear + 1;
  String Sd = String.valueOf(dayOfMonth + "/" + monthOfYear + "/" + year);
  tv.setText(Sd);
}

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

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