2

美好的一天,似乎无法找到解决方案。我有 2 个按钮,按下时会打开一个 datepickerdialog,但我现在的问题是,如何将它们设置为 OnDateSet 方法上的适当按钮。我使用 DialogFragment 作为日期,然后在我的活动中实现 DateListener。我曾尝试使用 getTag() 但没有成功获取标签。这是我尝试过的:

public void showfromDatePickerDialog(View v) {
        DialogFragment dateFragment = new DateDialogFragment();
        dateFragment.show(getSupportFragmentManager(), "fromdatePicker");
    }

    public void showtoDatePickerDialog(View v) {
        DialogFragment dateFragment = new DateDialogFragment();
        dateFragment.show(getSupportFragmentManager(), "todatePicker");
    }


        public void onDateSet(DatePicker view, int year, int month, int day) {

        StringBuilder builder = new StringBuilder();
        builder.append(day).append("-")
               .append(month).append("-")
               .append(year);

        String text= builder.toString();

        if(view.getTag().toString().equals("fromdatePicker")) { // error here

            Log.d(TAG, "got here" + text);
            fromdate.setText(text);
        }


        if(view.getTag() == "todatePicker") {
            todate.setText(text);
        }

任何想法如何实施?我不断看到有关使用 2 个不同 DialogFragment 类的解决方案,但我猜应该有另一种方法。还是我错了?谢谢

4

3 回答 3

4

好的,我可以解决这些问题。这对任何有这个问题的人都有帮助。如果它稍微不正确,请告诉我,我可以更改它。但这就是我为解决这个问题所做的。

在日期集上:

  public void onDateSet(DatePicker view, int year, int month, int day) {

        StringBuilder builder = new StringBuilder();
        builder.append(day).append("-")
               .append(month).append("-")
               .append(year);

        String text= builder.toString();

FragmentManager fragmanager = getSupportFragmentManager();

      if(fragmanager.findFragmentByTag("fromdatePicker") != null) {

            Log.d(TAG, "got here" + text);
            fromdate.setText(text);
        }



       // if(view.getTag() == "todatePicker") {
  if(fragmanager.findFragmentByTag("todatePicker") != null) {
            todate.setText(text);
        }

这样,您可以将相同的 dateListener 用于多个调用,并根据在对话框上调用 show 时传递的标签适当地设置日期。

于 2012-09-20T14:44:07.437 回答
1

使用下面的 DatePicker 对话框代码。

private int mYear;
private int mMonth;
private int mDay; 
Calendar cal4DatePicker = Calendar.getInstance();
Button btnDOB=findviewbyId(R.id.btndob);

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        mYear = year;
        mMonth = monthOfYear;
        mDay = dayOfMonth;

        cal4DatePicker.set(Calendar.YEAR, mYear);
        cal4DatePicker.set(Calendar.MONTH, mMonth);
        cal4DatePicker.set(Calendar.DAY_OF_MONTH, mDay);

        btnDOB.setText(new StringBuilder()
                .append(mDay).append("-").append(mMonth).append("-").append(mYear).append(" "));
    }
};

在 btnDOB 上,您需要设置单击列表器以显示此 DatePicker 对话框。

于 2012-09-20T14:11:11.707 回答
1

我只用时间选择器尝试过同样的事情。您也可以尝试为日期选择器设置相同的设置。

    package com.example.toolboxtest;


import java.text.Format;
import java.text.SimpleDateFormat;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;

import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TimePicker;

public class MainActivity extends FragmentActivity implements OnClickListener {

    Button testBtn; 
    EditText testET;
    static final int TIME_DIALOG_ID = 0;
    int mHour = 0;
    int mMinute = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        testBtn = (Button) findViewById(R.id.testBTn);
        testET = (EditText) findViewById(R.id.testET);

        testBtn.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
        case R.id.testBTn:
            showDialog(TIME_DIALOG_ID);
            break;
        }

    }
    private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            mHour=hourOfDay;
            mMinute = minute;
//          myPrefs = ImportantDateReminderActivity.this.getSharedPreferences("myPrefs",MODE_WORLD_WRITEABLE);
//          prefsEditor = myPrefs.edit(); 
//          prefsEditor.putInt("hour", mHour);
//          prefsEditor.putInt("minute", mMinute);
//          prefsEditor.putString("a", a);
//          prefsEditor.commit();
            if(mHour==12){                 
                //reminderSetTime.setText(new StringBuilder().append("12").append(":").append((minute)+" PM"));
                testET.setText(mHour+":"+mMinute+" "+"PM");

            }
            else             
                updateTimeDisplay();         
        } 
    };

    private void updateTimeDisplay() {          
        try {             
            Format formatter;             
            SimpleDateFormat df = new SimpleDateFormat("hh:mm");             
            Date d = df.parse(mHour + ":" + mMinute);             
            Calendar gc = new GregorianCalendar();             
            gc.setTime(d);             
            //gc.add(Calendar.HOUR, 0);             
            Date d2 = gc.getTime();  

            formatter = new SimpleDateFormat("hh:mm a");             
            String time = formatter.format(d2);   
            System.out.println("The TIME is: "+time);
            testET.setText(time);

            String hour = new SimpleDateFormat("hh").format(d2);
            String minute = new SimpleDateFormat("mm").format(d2);
            String a = new SimpleDateFormat("a").format(d2);
            System.out.println("The Hour is: "+hour+ " "+minute+ " " +a);
            /*myPrefs = this.getSharedPreferences("myPrefs",MODE_WORLD_WRITEABLE);
            prefsEditor = myPrefs.edit(); 
            prefsEditor.putInt("hour", Integer.parseInt(hour));
            prefsEditor.putInt("minute", Integer.parseInt(minute));
            prefsEditor.putString("a", a);
            prefsEditor.putString("complateTime", time);
            prefsEditor.commit();*/

            //addTwoMonthNotification();
        } catch (Exception e) {             
            e.printStackTrace();         
        }      
    } 

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case TIME_DIALOG_ID:
                return new TimePickerDialog(this,mTimeSetListener, mHour, mMinute, false);
        }
        return null;
    }

}

希望它会帮助你。

如果有任何问题,请告诉我。

于 2013-12-09T09:30:17.080 回答