如何获得此方法的返回

时间:2018-04-05 15:07:45

标签: android oop

我使用lib betterpickers,我试图创建一个类以OOP形式获取其信息,但该方法没有返回,它是无效的,有没有办法得到这个返回?

public class Inflate implements CalendarDatePickerDialogFragment.OnDateSetListener{

private static final String FRAG_TAG_DATE_PICKER = "fragment_date_picker_name";

public void piker(FragmentManager supportFragmentManager){
    CalendarDatePickerDialogFragment cdp = new CalendarDatePickerDialogFragment()
            .setOnDateSetListener(this);
    cdp.show(supportFragmentManager, FRAG_TAG_DATE_PICKER);
}

@Override
public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) {
   //This is the method
}
}

我在MainActivity中将其称为:

Inflate inflate = new Inflate();

tes.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           inflate.piker(getSupportFragmentManager());
        }
    });

我已经尝试过类似onDateSet的调用另一种方法:

@Override
public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) {
    getData(year + "/" + monthOfYear + "/" + dayOfMonth);
}

public String getData(String data){
    return data;
}

但如果我打电话给Piker,我怎么能得到这个反馈呢?这就是问题所在,有什么办法吗?因为在这个活动中我有4个地方必须定义一个日期,如果每个我必须做一个不同的方法代码变得庞大。

文档:https://github.com/code-troopers/android-betterpickers

1 个答案:

答案 0 :(得分:1)

最后我做了,我改变了方法,我这样做了:

public class Inflate{

    private static final String FRAG_TAG_DATE_PICKER = "fragment_date_picker_name";

    public void piker(FragmentManager supportFragmentManager, final TextView textView){
        CalendarDatePickerDialogFragment cdp = new CalendarDatePickerDialogFragment();
                cdp.setOnDateSetListener(new CalendarDatePickerDialogFragment.OnDateSetListener() {
                    @Override
                    public void onDateSet(CalendarDatePickerDialogFragment dialog, int year, int monthOfYear, int dayOfMonth) {
                        String data = year + "/" + monthOfYear + "/" + dayOfMonth;                 //After collecting the date I call the method in MainActivity
                        MainActivity.setData(data, textView);
                    }
                });

        cdp.show(supportFragmentManager, FRAG_TAG_DATE_PICKER);
    }
}

我称之为:

Inflate inflate = new Inflate();
tes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             //The date sets the value in the TextView, so I step to call the method
               inflate.piker(getSupportFragmentManager(), tes);
            }
        });

在MainActivity中,该方法接收并将值分配给TextView:

public static void setData(String data, TextView textView) {
        textView.setText(data);
}

谢谢@ADM