gpt4 book ai didi

android - TimePickerDialog 未显示/显示

转载 作者:行者123 更新时间:2023-11-29 22:01:12 26 4
gpt4 key购买 nike

我有一个 TimePickerDialog,我想在用户按下按钮时向其显示,以便她可以选择一个时间来设置显示(当前时间以外的某个时间)。当前,当按下按钮时,当前时间显示在 TextView 和 Toast 中。屏幕也会变暗,屏幕中央会出现一个小框。 TimePickerDialog 不向用户显示。

这是代码;

import java.util.Calendar;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import android.text.format.DateFormat;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class AddAlarmActivity extends FragmentActivity implements TimePickerDialog.OnTimeSetListener, TimePickerDialog.OnDismissListener {

Button setTime;
public int hour_local;
public int minute_local;
public Dialog onCreateDialog;
public TimePicker timePicker;
public TextView displayAlarm;

static final int TIME_DIALOG_ID = 999;

@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_alarm);
//add a TextView thing to this part

setTime = (Button) findViewById(R.id.button_set_time);
displayAlarm = (TextView) findViewById(R.id.tvDisplayTime);

setTime.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
onCreateDialog(savedInstanceState);
showTimePickerDialog(timePicker);
displayAlarm.setText(hour_local + ":" + minute_local);
}
});


}

public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current time as the default values for the picker

final Calendar c = Calendar.getInstance();
hour_local = c.get(Calendar.HOUR_OF_DAY);
minute_local = c.get(Calendar.MINUTE);

Context context = getApplicationContext();
CharSequence text = hour_local + ":" + minute_local;
int duration = Toast.LENGTH_LONG;

Toast toast = Toast.makeText(context, text, duration);
toast.show();

// Create a new instance of TimePickerDialog and return it
return new TimePickerDialog(getApplicationContext(), this, hour_local, minute_local,
DateFormat.is24HourFormat(getApplicationContext()));
}

public void showTimePickerDialog(View v) {
DialogFragment newFragment = new DialogFragment();
newFragment.show(getSupportFragmentManager(), "timePicker");
}


public void onDismiss(DialogInterface dialog){

System.out.println("oui c'est très bon !");

}

public void onTimeSet(TimePicker view, int hour_l, int minute_l) {
hour_local = hour_l;
minute_local = minute_l;
System.out.println("voila");
}
}

最佳答案

我建议您遵循开发者网站的做法:

  • 创建一个处理对话 fragment 的静态类

    public static class TimePickerFragment extends DialogFragment
    implements TimePickerDialog.OnTimeSetListener {

    @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
    }
    }
  • 在点击监听器的按钮中调用此方法:

    public void showTimePickerDialog(View v) {
    DialogFragment newFragment = new TimePickerFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");
    }

来源here .

关于android - TimePickerDialog 未显示/显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11892475/

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