简体   繁体   English

如何从另一个内部 class 获取变量以供全局使用

[英]How to get variable from another inner class for globally use

This is my code.这是我的代码。 Where have an inner class named BirthDatePickerFragment .其中有一个名为BirthDatePickerFragment的内部 class 。 I want to call globally birthYear , birthMonth , birthDay variables form onCreateDialog method which is under BirthDatePickerFragment .我想在BirthDatePickerFragment下的onCreateDialog方法中调用全局birthYearbirthMonthbirthDay变量。

MainActivity.java

import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button birthDatePickerButton;

    TextView calculateTextView;

    EditText birthDayDatePickerEditText;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        birthDatePickerButton = findViewById(R.id.birthDatePickerBT);
        birthDayDatePickerEditText = findViewById(R.id.birthDayDatePickEditTextID);
        calculateTextView=findViewById(R.id.calculateTextViewID);

        birthDatePickerButton.setOnClickListener(this);

    }


 public void onClick(View v) {


            // Initialize a new date picker dialog fragment
             DialogFragment dFragment = new BirthDatePickerFragment();

             // Show the date picker dialog fragment
             dFragment.show(getFragmentManager(), "Date Picker");


        }



public static class BirthDatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {


        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Calendar calendar = Calendar.getInstance();
            int birthYear = calendar.get(Calendar.YEAR);
            int birthMonth = calendar.get(Calendar.MONTH);
            int birthDay = calendar.get(Calendar.DAY_OF_MONTH);


            DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
                    AlertDialog.THEME_TRADITIONAL, this, birthYear, birthMonth,birthDay);
            return datePickerDialog;
        }


        public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

            // Do something with the chosen date
            EditText birthDatText=getActivity().findViewById(R.id.birthDayDatePickEditTextID);


            // Create a Date variable/object with user chosen date
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(0);
            cal.set(year, month, dayOfMonth, 0, 0, 0);
            Date chosenDate = cal.getTime();

            // Format the date using style short
            DateFormat df_short = DateFormat.getDateInstance(DateFormat.SHORT);
            String df_short_str = df_short.format(chosenDate);
            // Display the formatted date
            birthDatText.setText(df_short_str);


          }

You can think of your code more simply.您可以更简单地考虑您的代码。 If you ignore the outer class and just focus on the inner class, you'll see that onCreateDialog() is a public method of class BirthDatePickerFragment and it returns a Dialog object. If you ignore the outer class and just focus on the inner class, you'll see that onCreateDialog() is a public method of class BirthDatePickerFragment and it returns a Dialog object. As with all variables created inside Java methods, they are cleared when the method is finished.与在 Java 方法中创建的所有变量一样,当方法完成时它们会被清除。

If you want to get values of birthYear , birthMonth , birthDay , you'll have to access them through the Dialog object returned by the method.如果要获取birthYearbirthMonthbirthDay的值,则必须通过该方法返回的Dialog object 访问它们。 This might be possible since the variables are passed into the object's constructor.这可能是可能的,因为变量被传递到对象的构造函数中。

Everything said, I agree with the comments that you should think about what you are trying to do first, and see if the code can be structured differently to suit your purpose.一切都说,我同意你应该首先考虑你想要做什么的评论,看看代码是否可以不同的结构来满足你的目的。

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

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