我正在制作提醒应用程序。它有一个活动,您可以在其中看到警报,并在其中有一个浮动操作按钮以添加新警报。从该按钮打开我的自定义对话框以设置警报。应该返回TimePicker中选择的小时和分钟,但返回当前的小时和分钟
这是课程:
public class AlarmKurDialog extends AppCompatDialogFragment {
TimePicker timePicker;
SeekBar seekBar;
TextView bardakAdetiTxt;
Button alarmEkleBtn;
int quantity;
int hour;
int minute;
AlarmKurDialogListener listener;
public interface AlarmKurDialogListener{
void alarmBilgisiGetir(int hour, int minute, int quantity);
}
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_alarm_kur, null);
builder.setView(view);
timePicker = view.findViewById(R.id.timePicker1);
seekBar = view.findViewById(R.id.seekBar);
bardakAdetiTxt = view.findViewById(R.id.txt_adet);
alarmEkleBtn = view.findViewById(R.id.button);
timePicker.setIs24HourView(true);
if (Build.VERSION.SDK_INT >= 23 ) {
// getHour and getMinute returns current hour and minute
hour = timePicker.getHour();
minute = timePicker.getMinute();
Log.i("mert", "getHour:getMinute - " + hour + ":" + minute);
}
else {
hour = timePicker.getCurrentHour();
minute = timePicker.getCurrentMinute();
Log.i("mert", "getCurrentHour:getCurrentMinute - " + hour + ":" + minute);
}
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
quantity = 1 + progress;
bardakAdetiTxt.setText(String.valueOf(quantity));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {}
});
alarmEkleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
listener.alarmBilgisiGetir(hour, minute, quantity);
dismiss();
}
});
return builder.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
listener = (AlarmKurDialogListener) context;
}catch (ClassCastException e){
throw new ClassCastException(context.toString() + "AlarmKurDialogListener implement etmek gerekli");
}
}}
答案 0 :(得分:2)
尝试:
alarmEkleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hour = timePicker.getHour();
minute = timePicker.getMinute();
listener.alarmBilgisiGetir(hour, minute, quantity);
dismiss();
}
});
答案 1 :(得分:1)
这部分代码位于onCreateDialog
方法的内部:
if (Build.VERSION.SDK_INT >= 23 ) {
// getHour and getMinute returns current hour and minute
hour = timePicker.getHour();
minute = timePicker.getMinute();
Log.i("mert", "getHour:getMinute - " + hour + ":" + minute);
}
else {
hour = timePicker.getCurrentHour();
minute = timePicker.getCurrentMinute();
Log.i("mert", "getCurrentHour:getCurrentMinute - " + hour + ":" + minute);
}
这意味着您正在对话框创建的时刻向hour
和minute
分配一个值,此时该对话框将返回当前时间。您要做的是将整个代码块移到点击监听器上的提交按钮内:
alarmEkleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// --------------->>>>>PUT THAT CODE HERE INSTEAD! <<<<<-------------------------
listener.alarmBilgisiGetir(hour, minute, quantity);
dismiss();
}
});
这样,您实际上可以从选择器中获取最新的设置值。