2013-12-11 72 views
0

我有兩個線性佈局的列表視圖,並且有一個按鈕添加新行。當我點擊添加新行按鈕時,我想動態地創建新的按鈕行。之後,點擊那個創建的按鈕,我想顯示一個時間選擇器對話框。當用戶點擊設置時間按鈕,我想在那個按鈕中設置時間。我的問題是所有的按鈕(與不同的ID)添加罰款,當點擊該按鈕時間選擇器對話框彈出。但點擊設置時間按鈕後,時間不會設置。我該如何添加這個時間到按鈕。我怎樣才能處理這個按鈕?Android - 動態在列表視圖中添加新按鈕

這是我的一段代碼列表視圖onscroll聽者的內部

final LinearLayout l1 = LinearLayout) List_Layout.getChildAt(0); 
LinearLayout l12 = (LinearLayout) List_Layout.getChildAt(1);  
final Button button = new Button(getApplicationContext()); 
final Button button1 = new Button(getApplicationContext()); 

add_button.setOnClickListener(new OnClickListener() 
{ 
    public void onClick(View v) 
    { 
     button.setLayoutParams(lparams); 
     button1.setLayoutParams(lparams); 
     button.setId(0); 
     button1.setId(1); 
     l1.addView(button, lparams); 
     l12.addView(button1, lparams); 
    } 
} 
button.setOnClickListener(new OnClickListener() 
{ 
    @SuppressWarnings("deprecation") 
    public void onClick(View v) 
    { 
     showDialog(TIME_DIALOG_ID); 
    } 
} 
button1.setOnClickListener(new OnClickListener() 
{ 
    @SuppressWarnings("deprecation") 
    public void onClick(View v) 
    { 
     // TODO Auto-generated method stub 
     Log.e("tag 1", button1.getTag()+""); 
     showDialog(TIME_DIALOG_ID); 
    } 
}); 

爲timepicker對話框代碼的休息是正常工作

這只是示例代碼

請人幫我脫身從這個謎語。

enter image description here

編輯:

static final int TIME_DIALOG_ID = 998; 
private int hour; 
    private int minute; 
@Override 
    protected Dialog onCreateDialog(int id) { 
     switch (id) { 
case TIME_DIALOG_ID: 

      return new TimePickerDialog(this, 
        timePickerListener, hour, minute,false); 
} 
return null; 
} 
private TimePickerDialog.OnTimeSetListener timePickerListener = 
      new TimePickerDialog.OnTimeSetListener() { 
     public void onTimeSet(TimePicker view, int selectedHour, 
       int selectedMinute) { 
hour = selectedHour; 
      minute = selectedMinute; 
} 
} 
+0

其中是設置時間的代碼? – Triode

+0

我已添加時間碼查看我的編輯。 – Amsheer

+0

將點擊的按鈕保存在變量中。一旦時間被設置,設置按鈕的文本。 – Triode

回答

0

你需要設置的buttonbutton1文本到實際上是由用戶挑選的時間。爲此,請在timePickerListener中輸入button.setText(<formatted time>)

下面是一些代碼行,我用格式的日期和時間從java.util.Date對象:

Date date = new Date(); 
java.text.DateFormat dateFormat = DateFormat.getMediumDateFormat(context); 
java.text.DateFormat timeFormat = DateFormat.getTimeFormat(context); 
button.setText(String.format("%s %s", dateFormat.format(date), timeFormat.format(date))); 

在你的時間選擇器,你可以只格式化所選的值轉換爲字符串的情況:

button.setText(String.format("%02d:%02d", selectedHour, selectedMinute)); 
+0

button.settext(「」)將工作我知道。我該如何處理TimePickerDialog.OnTimeSetListener方法中動態創建的按鈕 – Amsheer

+0

所有你需要的是'button'的句柄。將其定義爲Activity類的'private',並且可以引用它。 – jboi

相關問題