-1

I hope someone can help me. I lack in OOP knowledge and am a newbie in android.

I have 6 buttons. Each button will call the timePicker and display the time on the button. How do i want to differentiate each buttons?

Thank you.....

Here's the code..:

public class TabTwo extends Activity implements OnClickListener {

private Button btnBrfkst;
private Button btnMtea;
private Button btnLunch;
private Button btnAftea;
private Button btnDinner;
private Button btnSupper;

private Calendar mCalen;
private int hourOfDay;
private int minute;
private int ampm;
int timePickerInput ;

private static final int Time_PICKER_ID = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab_two);

    btnBrfkst = (Button) findViewById(R.id.button1);
    btnMtea = (Button) findViewById(R.id.button2);
    btnLunch = (Button) findViewById(R.id.button3);
    btnAftea = (Button) findViewById(R.id.button4);
    btnDinner = (Button) findViewById(R.id.button5);
    btnSupper = (Button) findViewById(R.id.button6);

    mCalen = Calendar.getInstance();
    hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
    minute = mCalen.get(Calendar.MINUTE);
    ampm = mCalen.get(Calendar.AM_PM);

    btnBrfkst.setOnClickListener(this);
    btnMtea.setOnClickListener(this);
    btnLunch.setOnClickListener(this);
    btnAftea.setOnClickListener(this);
    btnDinner.setOnClickListener(this);
    btnSupper.setOnClickListener(this);
}

@Override
@Deprecated
protected Dialog onCreateDialog(int id) {

    switch (id) {
        case Time_PICKER_ID:
            return new TimePickerDialog(this, TimePickerListener,
                    hourOfDay, minute, false);
    }
    return null;
}

private TimePickerDialog.OnTimeSetListener TimePickerListener =
        new TimePickerDialog.OnTimeSetListener() {

            // while dialog box is closed, below method is called.
            public void onTimeSet(TimePicker view, int hour, int minute) {

                switch (timePickerInput) {
                    case R.id.button1:

                mCalen.set(Calendar.HOUR_OF_DAY, hour);
                mCalen.set(Calendar.MINUTE, minute);

                int hour12format = mCalen.get(Calendar.HOUR);
                hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
                minute = mCalen.get(Calendar.MINUTE);
                ampm = mCalen.get(Calendar.AM_PM);
                String ampmStr = (ampm == 0) ? "AM" : "PM";

                // Set the Time String in Button
                btnBrfkst.setText(hour12format + " : " + minute + " / " + ampmStr);
                break;

                case R.id.button2:
                mCalen.set(Calendar.HOUR_OF_DAY, hour);
                mCalen.set(Calendar.MINUTE, minute);

                 int hour12format2 = mCalen.get(Calendar.HOUR);
                 hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
                 minute = mCalen.get(Calendar.MINUTE);
                 ampm = mCalen.get(Calendar.AM_PM);
                 String ampmStr2 = (ampm == 0) ? "AM" : "PM";
                 btnMtea.setText(hour12format2 + " : " + minute + " / " + ampmStr2);
                 break;

                case R.id.button3:
                mCalen.set(Calendar.HOUR_OF_DAY, hour);
                mCalen.set(Calendar.MINUTE, minute);

                 int hour12format3 = mCalen.get(Calendar.HOUR);
                  hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
                 minute = mCalen.get(Calendar.MINUTE);
                  ampm = mCalen.get(Calendar.AM_PM);
                  String ampmStr3 = (ampm == 0) ? "AM" : "PM";
                 btnLunch.setText(hour12format3 + " : " + minute + " / " + ampmStr3);
                break;

                    case R.id.button4:
                    mCalen.set(Calendar.HOUR_OF_DAY, hour);
                    mCalen.set(Calendar.MINUTE, minute);

                    int hour12format4 = mCalen.get(Calendar.HOUR);
                    hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
                    minute = mCalen.get(Calendar.MINUTE);
                    ampm = mCalen.get(Calendar.AM_PM);
                    String ampmStr4 = (ampm == 0) ? "AM" : "PM";
                    btnAftea.setText(hour12format4 + " : " + minute + " / " + ampmStr4);
                break;

                    case R.id.button5:
                        mCalen.set(Calendar.HOUR_OF_DAY, hour);
                        mCalen.set(Calendar.MINUTE, minute);

                        int hour12format5 = mCalen.get(Calendar.HOUR);
                        hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
                        minute = mCalen.get(Calendar.MINUTE);
                        ampm = mCalen.get(Calendar.AM_PM);
                        String ampmStr5 = (ampm == 0) ? "AM" : "PM";
                        btnDinner.setText(hour12format5 + " : " + minute + " / " + ampmStr5);
                break;

                    case R.id.button6:
                        mCalen.set(Calendar.HOUR_OF_DAY, hour);
                        mCalen.set(Calendar.MINUTE, minute);

                        int hour12format6 = mCalen.get(Calendar.HOUR);
                        hourOfDay = mCalen.get(Calendar.HOUR_OF_DAY);
                        minute = mCalen.get(Calendar.MINUTE);
                        ampm = mCalen.get(Calendar.AM_PM);
                        String ampmStr6 = (ampm == 0) ? "AM" : "PM";
                btnSupper.setText(hour12format6 + " : " + minute + " / " + ampmStr6);
                break;
                }

            }
        };

@Override
public void onClick(View v) {
    showDialog(Time_PICKER_ID);
}
}
4

2 回答 2

3

您的活动或片段应该implements View.OnClickListener。然后每个按钮应该像这样注册每个按钮:

Button buttonOne = (Button) findViewById(R.id.button_one); 
Button buttonTwo = (Button) findViewById(R.id.button_two);

buttonOne.setOnClickListener(this); 
buttonTwo.setOnClickListener(this);

然后在您的 onClick 方法中:

@Override
public void onClick(View temp)
{
    switch(temp.getId()) {
       case R.id.button_one:
            //do something
            break;
       case R.id.button_two:
            //do something
            break;

    }
} 

另一个选项是设置在布局文件中为每个按钮调用的方法,使用android:onClick="myMethodOne". 这样,您可以根据需要调用不同的方法,甚至可以调用相同的方法,并像在 onClick 方法中那样区分 id。

于 2013-08-30T02:43:44.873 回答
0

改变这个:

@Override
public void onClick(View v) {
    showDialog(Time_PICKER_ID);
}

@Override
public void onClick(View v) {
    timePickerInput = v.getId();
    showDialog(Time_PICKER_ID);
}

您需要将 timerPickerInput 设置为按钮的 id,以便您知道在哪个按钮中显示时间。

于 2013-08-30T05:36:00.360 回答