gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 12:38:11 24 4
gpt4 key购买 nike

我是 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com