2

我有以下代码。在这个我有动态生成的按钮。当单击按钮时,它会显示时间选择器。我的问题是设置单击按钮的时间,但它设置了最后一个按钮的文本如何设置单击按钮的值...

public void addButton(int value) {
    list.removeAllViews();

    for (i=0 ; i < value; i++) {
        button = new Button(this);
        button.setText("Add Time");
        button.setId(i);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Calendar mCalendar = Calendar.getInstance();
                new TimePickerDialog(MedInfo.this, onTimeListener, mCalendar
                        .get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), false)
                        .show();
                btn_cnt = v.getId();
                Toast.makeText(getApplicationContext(), ""+btn_cnt, Toast.LENGTH_SHORT).show();
            }
        });
        list.addView(button);
    }
}


TimePickerDialog.OnTimeSetListener onTimeListener = new OnTimeSetListener() {

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        String AM_PM;
        Calendar mCalendar = Calendar.getInstance();
        if (hourOfDay < 12) {
            AM_PM = "AM";

        } else {
            AM_PM = "PM";

        }

        long start_time = DateTimeUtils.getTimeInMillis(hourOfDay, minute, 0);
        Calendar calendar = Calendar.getInstance();

        long mseconds = DateTimeUtils.getFirstMillis(calendar.get(Calendar.DATE),
                calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR));
        time = start_time + mseconds;

        button.settext(time);
        mCalendar.set(Calendar.HOUR, hourOfDay);
        mCalendar.set(Calendar.MINUTE, minute);

    }
};

任何帮助将是可观的...

编辑 :

错误日志:

10-29 15:07:32.478: E/AndroidRuntime(6401): FATAL EXCEPTION: main
10-29 15:07:32.478: E/AndroidRuntime(6401): java.lang.NullPointerException
10-29 15:07:32.478: E/AndroidRuntime(6401):     at com.example.medicationreminder.MedInfo$MyOnTimeSetListenter.onTimeSet(MedInfo.java:237)
10-29 15:07:32.478: E/AndroidRuntime(6401):     at android.app.TimePickerDialog.tryNotifyTimeSet(TimePickerDialog.java:130)
10-29 15:07:32.478: E/AndroidRuntime(6401):     at android.app.TimePickerDialog.onClick(TimePickerDialog.java:115)
4

3 回答 3

2

一种可能的解决方案是实现一个自定义OnTimeSetListener,它可以保持对单击按钮的引用。在显示时间选择器之前,通知自定义侦听器单击了哪个按钮。请参见下面的代码:

public void addButton(int value) {
    list.removeAllViews();

    for (i=0 ; i < value; i++) {
        button = new Button(this);
        button.setText("Add Time");
        button.setId(i);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Calendar mCalendar = Calendar.getInstance();
                // set the button which was clicked
                onTimeListener.setClickedButton((Button)v);
                new TimePickerDialog(this, onTimeListener, mCalendar
                        .get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), false)
                        .show();
                btn_cnt = v.getId();
                Toast.makeText(getApplicationContext(), ""+btn_cnt, Toast.LENGTH_SHORT).show();
            }
        });
        list.addView(button);
    }
}

TimePickerDialog.OnTimeSetListener onTimeListener = new MyOnTimeSetListenter();

class MyOnTimeSetListenter implements TimePickerDialog.OnTimeSetListener {

    private Button button;

    public void setClickedButton(Button button) {
        this.button = button;
    }

    @Override
    public void onTimeSet(TimePicker timePicker, int i, int i2) {
        String AM_PM;
        Calendar mCalendar = Calendar.getInstance();
        if (hourOfDay < 12) {
            AM_PM = "AM";

        } else {
            AM_PM = "PM";

        }

        long start_time = DateTimeUtils.getTimeInMillis(hourOfDay, minute, 0);
        Calendar calendar = Calendar.getInstance();

        long mseconds = DateTimeUtils.getFirstMillis(calendar.get(Calendar.DATE),
                calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR));
        time = start_time + mseconds;

        this.button.settext(time);
        mCalendar.set(Calendar.HOUR, hourOfDay);
        mCalendar.set(Calendar.MINUTE, minute);
    }
}
于 2013-10-29T09:26:38.573 回答
2

尝试更新特定按钮的文本

Button b=(Button)list.getChildAt(btn_cnt);
b.settext(time);

当您存储Button Idbtn_cnt中时,可以使用getChildAt(position)

where list是您Layout添加Button dynamically.

public void addButton(int value) {
    list.removeAllViews();

    for (i=0 ; i < value; i++) {
        button = new Button(this);
        button.setText("Add Time");
        button.setId(i);

        button.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Calendar mCalendar = Calendar.getInstance();
                new TimePickerDialog(MedInfo.this, onTimeListener, mCalendar
                        .get(Calendar.HOUR_OF_DAY), mCalendar.get(Calendar.MINUTE), false)
                        .show();
                btn_cnt = v.getId();
                Toast.makeText(getApplicationContext(), ""+btn_cnt, Toast.LENGTH_SHORT).show();
            }
        });
        list.addView(button);
    }
}


TimePickerDialog.OnTimeSetListener onTimeListener = new OnTimeSetListener() {

    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        String AM_PM;
        Calendar mCalendar = Calendar.getInstance();
        if (hourOfDay < 12) {
            AM_PM = "AM";

        } else {
            AM_PM = "PM";

        }

        long start_time = DateTimeUtils.getTimeInMillis(hourOfDay, minute, 0);
        Calendar calendar = Calendar.getInstance();

        long mseconds = DateTimeUtils.getFirstMillis(calendar.get(Calendar.DATE),
                calendar.get(Calendar.MONTH), calendar.get(Calendar.YEAR));
        time = start_time + mseconds;

        Button b=(Button)list.getChildAt(btn_cnt);
        b.settext(time);

        mCalendar.set(Calendar.HOUR, hourOfDay);
        mCalendar.set(Calendar.MINUTE, minute);

    }
};

编辑:- 您可以使用List而不是Array一直从 Button 检索。

List<String> listOfDate=new ArrayList<String>();
        for(int i=0;i<list.getChildCount();i++)
        {
            Button b=(Button)list.getChildAt(i);
            listOfDate.add(b.getText().toString());

        }

希望这会帮助你。

于 2013-10-29T09:33:53.563 回答
0

只需创建一个全局变量并在需要时设置其值 onTimeListener 和 setText(global_variable)

于 2013-10-29T09:34:26.947 回答