Java 完成时间选择器时调用非静态函数
我对安卓有点陌生,我现在正和时间选择器打交道。我这里有一个计时器:Java 完成时间选择器时调用非静态函数,java,android,datepicker,static,timepicker,Java,Android,Datepicker,Static,Timepicker,我对安卓有点陌生,我现在正和时间选择器打交道。我这里有一个计时器: public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener { public String time; @Override public Dialog onCreateDialog(Bundle savedInstanceState) {
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);
}
}
这是可行的,但在用户选择了一个时间后,我想调用活动中的一个函数,选择器正在更改按钮颜色和文本。这在一个名为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();
我尝试了这个,虽然它不再抛出错误,但当我在时间选择器中点击ok时,应用程序正在崩溃