1

I'm using two timepickers in the same activity to let the user choose a start time and a stop time for a specific task, I'm using the DialogFragment class that's available in the support library for backward compatibility with older Android versions.

I set the timepickers and they're showing up correctly, but I can't figure out how to get the values the user inputs for both of these timepickers, I don't have any background or experience with implementing dialogs so your guidance with code example is highly appreciated!

Here is my code to create the TimePickerFragment class, created in a seperate file, taken and edited slightly from: http://developer.android.com/guide/topics/ui/controls/pickers.html :

package com.calls.only;

import java.util.Calendar;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;

public class TimePickerFragment extends DialogFragment {

    @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);

        if (!(getActivity() instanceof OnTimeSetListener)) throw new IllegalStateException("Activity should implement OnTimeSetListener!");
            OnTimeSetListener timeSetListener =  (OnTimeSetListener) getActivity();

        // Create a new instance of TimePickerDialog and return it
        return new TimePickerDialog(getActivity(), timeSetListener, hour, minute, false);
    }
}

Inside My MainActivity:

package com.calls.only;
import java.util.Calendar;
import java.util.TimeZone;
import android.os.Bundle;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.DialogFragment;
import android.view.Menu;
import android.view.View;
import android.widget.RadioButton;
import android.widget.TextView;

public class MainActivity extends FragmentActivity implements TimePickerDialog.OnTimeSetListener {

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

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

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            Log.i("TimePicker", "Time picker set!");
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

上記のコードに何かを追加して、2 つのタイムピッカーを実装し、それらの入力を区別する方法についてのアイデアはありますか?

4

3 に答える 3

0

TimePickerDialog.OnTimeSetListenerアクティビティで実装する代わりに、コンストラクターでフラグメントに提供します。そうすれば、各インスタンスは異なることを実行できます。

したがって、次のように使用します。

  frag1 = new TimePickerFragment(new TimePickerDialog.OnTimeSetListener() { /* Your implementation*/ });
于 2013-04-10T19:30:25.860 に答える
0

editText の onClick() ダイアログがタイムピッカーで表示されるアクティビティ

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if ((v.getId() == R.id.edtTime)) {

        final Dialog dialog = new Dialog(AddTimeActivity.this);
        dialog.setContentView(R.layout.add_time_dialog);
        Button mSave = (Button) dialog.findViewById(R.id.btnSave);
        mSave.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                TimePicker timePicker = (TimePicker) dialog
                        .findViewById(R.id.timePicker1);
                timePicker.clearFocus();
                hourSelect = timePicker.getCurrentHour();
                minuteSelect = timePicker.getCurrentMinute();

                if (hourSelect == 0) {
                    tt = "AM";
                    hourSelect = 12;
                } else if (hourSelect > 0 && hourSelect < 12) {
                    tt = "AM";
                } else if (hourSelect == 12) {
                    tt = "PM";
                } else {
                    tt = "PM";
                    hourSelect = hourSelect - 12;
                }
                timeSelect = hourSelect;
                edtTime.setText(new         StringBuilder().append(timeSelect)
                          .append(":").append(pad(minuteSelect)).append(tt));
                dialog.dismiss();

            }
        });
        dialog.show();
    }

add_time_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Select Time" />

<TimePicker
    android:id="@+id/timePicker1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center" />

<Button
    android:id="@+id/btnSave"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:text=" Save " />

</LinearLayout>
于 2013-04-10T19:36:25.437 に答える