0

我是一名初学者 android 程序员,我有这个问题。

我有一堂课如下...

import java.util.Calendar;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.os.Bundle;
import android.widget.DatePicker;

public class DatePickerFragment extends DialogFragment implements
        DatePickerDialog.OnDateSetListener {

    private int year;
    private int month;
    private int day;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(),
                AlertDialog.THEME_DEVICE_DEFAULT_DARK, this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        this.setYear(year);
        this.setMonth(month);
        this.setDay(day);
        System.out.println(day+""+month+""+year);
        // Do something with the date chosen by the user
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }
}

在我的主要活动中,我有一个带有侦听器的按钮,当我按下它时,它会调用此类并打开对话框/日期选择器,如下所示:

setDoBButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                DatePickerFragment newFragment = new DatePickerFragment();
                newFragment.show(getFragmentManager(), "datePicker");
            }
        });

我的问题是,一旦我单击按钮并出现对话框,在我设置我的日期并被onDateSet调用后,我不能使用我的 getter 将日期分配给我的 mainactivity 中的任何变量,因为onclick它没有等待我完成设置对话框中的日期。

例如,如果我的听众是这样的......

    setDoBButton.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    DatePickerFragment newFragment = new DatePickerFragment();
                    newFragment.show(getFragmentManager(), "datePicker");
                    int x = newFragment.getDay();
                }
            });

它不起作用,因为对话框会显示,然后getDay会立即被调用,因为它在onclick对话框中但日期尚未设置。那么我该如何解决这个问题呢?

非常感谢。

4

2 回答 2

1

使用接口。

在 中定义一个接口DatePickerFragment。在您的活动类中实现接口并使用相同的接口。

检查下与活动通信的主题。

http://developer.android.com/guide/components/fragments.html

例子:

public class MainActivity extends Activity implements DatePickerFragment.TheListener{

    Button b;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b= (Button) findViewById(R.id.button1);
        tv= (TextView) findViewById(R.id.textView1);
        b.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                DialogFragment picker = new DatePickerFragment();
                picker.show(getFragmentManager(), "datePicker");
            }

        });
    }

    @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 returnDate(String date) {
        // TODO Auto-generated method stub
        tv.setText(date);
    }

}

日期选择器片段

public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {

    TheListener listener;

public interface TheListener{
    public void returnDate(String date);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
listener = (TheListener) getActivity(); 

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

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
if (listener != null) 
{
  listener.returnDate(formattedDate); // instead of date set the day

}

}
}

在上面的示例中,您在 DaterPicker 中有接口

 public interface TheListener{
    public void returnDate(String date);
 }

然后

 listener.returnDate(formattedDate);

然后你在你的活动类中实现监听器

 implements DatePickerFragment.TheListener

您实现该方法

  @Override
    public void returnDate(String date) {
        // TODO Auto-generated method stub
        tv.setText(date); // date is available here
    }
于 2013-10-11T10:39:32.137 回答
0

创建一个称为分配变量或类似方法的方法,并在onclick调用该方法时

setDoBButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                DatePickerFragment newFragment = new DatePickerFragment();
                newFragment.show(getFragmentManager(), "datePicker");
                assign_variables();
            }
        });

private void assign_variables(){
    int x = newFragment.getDay();
}

或者

创建一个static class variable喜欢go_on

private static final boolean go_on = true;

现在在你onclick添加一个无限循环

setDoBButton.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                DatePickerFragment newFragment = new DatePickerFragment();
                newFragment.show(getFragmentManager(), "datePicker");
                while(go_on){}
            }
        });

然后随心所欲地做你的任务

int x = newFragment.getDay();
int y = newFragment.getTime();
go_on = false; //and set the boolean to false

现在它会打破while循环,你会得到你想要的东西

注意:请注意不要以某种死锁或 ANR 错误结束

于 2013-10-11T10:47:01.707 回答