1

ユーザーがボタンを押したときに表示したい 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");
}
}
4

3 に答える 3

2

開発者サイトのやり方に従うことをお勧めします。

  • ダイアログフラグメントを処理する静的クラスを作成します

    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");
    }
    

ソースはこちら

于 2012-08-09T22:02:26.207 に答える
0
public class AddAlarmActivity extends FragmentActivity {

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

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_alarm);

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

    final Calendar c = Calendar.getInstance();
    hour_local = c.get(Calendar.HOUR_OF_DAY);
    minute_local = c.get(Calendar.MINUTE);
    displayAlarm.setText(hour_local + ":" + minute_local);

    setTime.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            showTimePickerDialog(timePicker);

        }
    });  
}

public void onTimeSet(TimePicker view, int hour_l, int minute_l) {
// intentionally blank
}

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

/*New static class*/
public static class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

    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 = getActivity();
        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(getActivity(), this, hour_local, minute_local,
                DateFormat.is24HourFormat(getActivity()));
    }

    public void onTimeSet(TimePicker view, int hour_l, int minute_l) {
        hour_alarm = hour_l;
        minute_alarm = minute_l;

        displayAlarm.setText(hour_alarm + ":" + minute_alarm);
        }

}
}
于 2012-08-10T21:29:54.530 に答える
0
TimePickerDialog dialog = new TimePickerDialog(this,OnTimeSet,DateTime.Now.Hour,DateTime.Now.Minute,true);
dialog.Show();

void OnTimeSet (object sender, TimePickerDialog.TimeSetEventArgs e)
{

}
于 2013-04-04T18:18:38.363 に答える