简体   繁体   中英

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. 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

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:

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:

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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