TimePickerDialog显示两次

时间:2016-01-12 16:18:43

标签: android datepicker timepicker

我正在使用Android项目,并希望让用户通过DatePicker和TimePicker设置日期和时间。

我想在设置日期后显示TimePicker。但是,TimePicker出现两次,我不知道原因。这是我的代码:

Create1Activity.java

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.app.TimePickerDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;

import android.app.DialogFragment;
import android.widget.TimePicker;
import android.widget.DatePicker;

import java.util.Calendar;

public class Create1Activity extends AppCompatActivity implements    TimePickerDialog.OnTimeSetListener,
    DatePickerDialog.OnDateSetListener{
// public final String[] headers = new String[]{"Player 1 ID","Player 2 ID","Bet","Bet Time"};
    public String strUserName = "";
    public int UserId;
    public int deal_no=0;
    public int year,month,day,hour,minute;
public String[][] arrGameTable ; //(match_id,description)




private TextView tvDisplayTime;
private TimePicker timePicker1;
private Button btnChangeTime;


static final int TIME_DIALOG_ID = 999;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create1);
    Bundle extras = getIntent().getExtras();
    //String username;
    if (extras == null) {
        strUserName = null;
    } else {
        strUserName = extras.getString("USERNAME");
        UserId=extras.getInt("USERID");
    }
    TextView txtUser = (TextView) findViewById(R.id.textUser);
    txtUser.setText(strUserName);
    final Calendar c = Calendar.getInstance();
    year = c.get(Calendar.YEAR);
    month = c.get(Calendar.MONTH);
    day = c.get(Calendar.DATE);
    hour = c.get(Calendar.HOUR_OF_DAY);
    minute = c.get(Calendar.MINUTE);
    //lm = (LinearLayout) findViewById(R.id.linearLayout_Table);
    //svrGetGame = new GetGameFromServer();
    //svrGetGame.execute((Void) null);


}
public void showTimePickerDialog() {
    DialogFragment newFragment = new TimePickerFragment();
    Bundle args=new Bundle();
    args.putInt("hour", hour);
    args.putInt("minute", minute);
    newFragment.setArguments(args);
    newFragment.show(getFragmentManager(), "timePicker");
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
    // Do something with the time chosen by the user
    Log.d("onTimeSet", String.valueOf(hourOfDay)+" "+String.valueOf(minute));
    this.hour=hourOfDay;
    this.minute=minute;
}
public void showDatePickerDialog(View v) {
    DatePickerFragment newFragment = new DatePickerFragment();
    Bundle args=new Bundle();
    args.putInt("year", year);
    args.putInt("month", month);
    args.putInt("day", day);
    newFragment.setArguments(args);
    newFragment.show(getFragmentManager(), "datePicker");
}
@Override
public void onDateSet(DatePicker view, int year, int month,int day) {
    // Do something with the time chosen by the user
    Log.d("onDateSet", String.valueOf(month)+" "+String.valueOf(day));
    this.year=year;
    this.month=month;
    this.day=day;
    showTimePickerDialog();
    /*
    DialogFragment newFragment = new TimePickerFragment();
    Bundle args=new Bundle();
    args.putInt("hour", hour);
    args.putInt("minute", minute);
    newFragment.setArguments(args);
    newFragment.show(getFragmentManager(), "timePicker");
    */
}





@Override
public void onStart() {
    super.onStart();

}

@Override
public void onStop() {
    super.onStop();

}


public void BtnBackClick(View view) {
    Log.d("BtnBackClick", "Go Select Page");
    finish();
}




    @Override
    protected void onCancelled() {
        //showProgress(false);
    }
}


}

DatePickerFragment.java:

import android.app.DialogFragment;
import android.app.Activity;
import android.app.Dialog;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.*;

import android.app.TimePickerDialog;
import android.text.format.DateFormat;


import android.os.Bundle;

public  class DatePickerFragment extends DialogFragment
     {
         DatePickerDialog.OnDateSetListener mListener;
         @Override

         public void onAttach(Activity activity) {
             super.onAttach(activity);
             // Verify that the host activity implements the callback interface
             try {

                 mListener = (DatePickerDialog.OnDateSetListener) activity;
             } catch (ClassCastException e) {
                 // The activity doesn't implement the interface, throw exception
                 throw new ClassCastException(activity.toString()
                         + " must implement onTimeListener");
             }
         }
         @Override
         public Dialog onCreateDialog(Bundle savedInstanceState) {
             // Use the current time as the default values for the picker
             int year = getArguments().getInt("year");
             int month = getArguments().getInt("month");
             int day = getArguments().getInt("day");

             // Create a new instance of TimePickerDialog and return it
             return new DatePickerDialog(getActivity(), mListener, year,month,day);
         }

}

TimePickerFragment.java:

import android.app.DialogFragment;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.*;

import android.text.format.DateFormat;


import android.os.Bundle;


public class TimePickerFragment extends DialogFragment {
OnTimeSetListener mListener;

// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener

@Override

public void onAttach(Activity activity) {
    super.onAttach(activity);
    // Verify that the host activity implements the callback interface
    try {

        mListener = (OnTimeSetListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(activity.toString()
                + " must implement onTimeListener");
    }
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current time as the default values for the picker
    int hour = getArguments().getInt("hour");
    int minute = getArguments().getInt("minute");

    // Create a new instance of TimePickerDialog and return it
    return new TimePickerDialog(getActivity(), mListener, hour, minute,
            DateFormat.is24HourFormat(getActivity()));
}

}

0 个答案:

没有答案
相关问题