关于onClick和android控制流的Android查询

时间:2013-10-11 10:35:07

标签: java android

我是初学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但未在对话框中设置日期。那么我该如何解决这个问题?

非常感谢。

2 个答案:

答案 0 :(得分: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);
    }

}

DatePickerFragment

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
    }

答案 1 :(得分:0)

创建一个名为assign variables或类似的方法 并在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错误结束