java—对同一包中的其他类不可见的公共方法

esbemjvw  于 2021-07-03  发布在  Java
关注(0)|答案(3)|浏览(341)

我有两节课。一个是我的主要活动,另一个是helper方法,它应该打开一个android日期选择器。到目前为止一切都很顺利,除了我想从日期选择器类中检索日期。以下是我的课程:

// This is the class that creates the date picker
    package com.strom.myapp;

    import android.app.DatePickerDialog;
    import android.app.Dialog;
    import android.app.DialogFragment;
    import android.os.Bundle;
    import android.widget.DatePicker;

    import java.util.Calendar;

    public class DefaultDatePicker extends DialogFragment implements DatePickerDialog.OnDateSetListener {

        private String returnDate;

        public String getReturnDate() {
            return returnDate;
        }

        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            return new DatePickerDialog(getActivity(), this, year, month, day);
        }

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            returnDate = String.valueOf(dayOfMonth) +
                    "." +
                    String.valueOf(monthOfYear) +
                    "." +
                    String.valueOf(year) +
                    ", 00:00";
        }
    }

这里是主要的活动

package com.strom.myapp;

import android.app.DialogFragment;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BalanceScreen extends ActionBarActivity {

private String payDate;

// ... omitted code ...

@Override
public boolean onOptionsItemSelected(MenuItem item) {

        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (id == R.id.action_payDate) {

            DialogFragment datePicker = new DefaultDatePicker();
            datePicker.show(getFragmentManager(), "datePicker");
            payDate = datePicker.getReturnDate();

            return true;
        }

// ... omitted code ...

}

getter方法'datepicker.getreturndate();'无法从主活动访问,即使它们位于同一个包中。我不明白为什么这不管用。我错过了什么?

0tdrvxhp

0tdrvxhp1#

你可以投 datePicker (引用defaultdatepicker的示例)到 DefaultDatePicker 调用特定于 DatePickerDialog.OnDateSetListener .

((DefaultDatePicker)datePicker).getReturnDate()
vwhgwdsa

vwhgwdsa2#

我建议您更换:

DialogFragment datePicker = new DefaultDatePicker();

具有

DefaultDatePicker datePicker = new DefaultDatePicker();

因为getreturndate方法是在defaultdatepicker中声明的,而不是在dialogfragment中。

w9apscun

w9apscun3#

向活动中添加公共变量

public String dateFromPicker = "";

DefaultDatePicker 方法 onDateSet 做这个

((BalanceScreen)getActivity()).dateFromPicker = returnDate;

相关问题