java - TimePicker 完成时调用非静态函数

标签 java android datepicker static timepicker

我是 Android 新手,目前正在努力使用 TimePicker。所以,我在这里有 TimePicker:

public class TimePickerFragment extends DialogFragment
    implements TimePickerDialog.OnTimeSetListener {

    public String time;
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current time as the default values for the picker
        final Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), this, hour, minute,
            DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        // Do something with the time chosen by the user
        time = hourOfDay + ":" + minute;
        //update global variable
        MockDB.setCheckout(time);
    }
}

这是有效的,但是在用户选择时间后,我想在选择器所在的 Activity 中调用一个函数来更改按钮颜色和文本。这是一个名为 ReserveProduct 的函数,该函数扩展了 AppCompatActivity。

public void animateButtons() {
    //picker disappears until next button is clicked
    Button picker = (Button) findViewById(R.id.button6);
    picker.setVisibility(View.GONE);
    Button picker1 = (Button) findViewById(R.id.button7);
    picker1.setVisibility(View.GONE);
    if (settingReturn == false) {
        //first button turns gray
        Button bttn1 = (Button) findViewById(R.id.buttonCheckIn);
        bttn1.setBackgroundResource(R.drawable.button_inactive);
        String time = ((MockDB) this.getApplication()).getCheckout();
        bttn1.setText("Check Out: 12:27 PM");
        //new button appears
        Button bttn2 = (Button) findViewById(R.id.buttonCheckOut);
        bttn2.setVisibility(View.VISIBLE);
        settingReturn = true;
    } else {
        //make 2nd button inactive
        Button bttn2 = (Button) findViewById(R.id.buttonCheckOut);
        bttn2.setBackgroundResource(R.drawable.button_inactive);
        String time = ((MockDB) this.getApplication()).getReturn();
        bttn2.setText("Return: 1:27 PM");
        //show new buttons
        Button set = (Button) findViewById(R.id.buttonSet);
        set.setVisibility(View.VISIBLE);
        Button home = (Button) findViewById(R.id.buttonHome);
        home.setVisibility(View.VISIBLE);

    }
}

我的问题是这个函数不是静态的,所以我不能简单地从 TimePicker 类调用它。我无法将按钮更改功能移至 TimePicker 类,因为我需要能够扩展 AppCompatActivity,但 AppCompatActivity 和 DialogFragment 有一个冲突的类。我也无法将 animateButtons() 类设为静态,因为这样 findViewById() 功能会引发错误。

请帮忙!!

最佳答案

首先实例化该类,即

(new SomeClass()).someMethod();

关于java - TimePicker 完成时调用非静态函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45058849/

相关文章:

android - 如何在 Android 8.0 Oreo 上以编程方式结束来电

jquery - JavaScript : jQuery Datepicker - simple highlighting of specific days, 谁能帮忙? (来源内)

java - 编辑文件后无法从虚拟目录访问 XML 文件

java - 简单的java程序根据它们的值来处理canopy簇字符串

java - 使用循环的 AsyncTask 示例

javascript - Bootstrap 日期选择器,禁用日期 - 可以查看仅模式吗?

javascript - 绑定(bind)到类时,JQuery UI 日期选择器不起作用

java - 直接生成二维码到ImageView?

java - 扫描二维码拍照

android - Phonegap摄像头android杀死了 Cordova