TimePicker.getHour() 和 TimePicker.getMinute() returns 当前小时和分钟
TimePicker.getHour() and TimePicker.getMinute() returns current hour and minute
我正在制作提醒应用。它有一个 activity ,您可以在其中看到您的警报和一个浮动操作按钮,用于添加新警报。从那个按钮我的自定义对话框打开设置闹钟。它应该 return 在 TimePicker 中选择的小时和分钟,但它 return 是当前的小时和分钟
这是class:
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");
}
}}
我在这里做错了什么?
试试这个:
alarmEkleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hour = timePicker.getHour();
minute = timePicker.getMinute();
listener.alarmBilgisiGetir(hour, minute, quantity);
dismiss();
}
});
这部分代码在 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
赋值,此时它只是 returns 当前时间。您想要做的是将整个代码块移动到 提交按钮 的点击侦听器中:
alarmEkleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// --------------->>>>>PUT THAT CODE HERE INSTEAD! <<<<<-------------------------
listener.alarmBilgisiGetir(hour, minute, quantity);
dismiss();
}
});
这样您实际上会从选择器中获得最新的设置值。
我正在制作提醒应用。它有一个 activity ,您可以在其中看到您的警报和一个浮动操作按钮,用于添加新警报。从那个按钮我的自定义对话框打开设置闹钟。它应该 return 在 TimePicker 中选择的小时和分钟,但它 return 是当前的小时和分钟
这是class:
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");
}
}}
我在这里做错了什么?
试试这个:
alarmEkleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
hour = timePicker.getHour();
minute = timePicker.getMinute();
listener.alarmBilgisiGetir(hour, minute, quantity);
dismiss();
}
});
这部分代码在 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
赋值,此时它只是 returns 当前时间。您想要做的是将整个代码块移动到 提交按钮 的点击侦听器中:
alarmEkleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// --------------->>>>>PUT THAT CODE HERE INSTEAD! <<<<<-------------------------
listener.alarmBilgisiGetir(hour, minute, quantity);
dismiss();
}
});
这样您实际上会从选择器中获得最新的设置值。