2013-03-29 22 views
0

我正在創建一個鬧鐘來播放音樂。我使用TimePicker顯示設置鬧鐘的時間,並創建了一個帶有監聽器的按鈕(Set Time)以從TimePicker和按鈕捕獲時間。我想在手機上使用TextView(lblAlarmTime)顯示時間。我無法獲取要在TextView中顯示的值。我不知道我的聽衆是不正確的還是我試圖錯誤地顯示文本。我想顯示在「您的鬧鐘設置爲:」標籤下捕捉到的時間。使用TextView無法顯示來自TimePicker的時間

我是Android編程新手。我遵循Android Developers/Pickers中的示例以及Buttons的偵聽器示例。不知道我錯過了什麼。

下面是XML文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:paddingLeft="16dp" 
android:paddingRight="16dp" 
tools:context=".SetAlarmActivity" > 

<TextView 
    android:id="@+id/alarm_picker_text1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center"   
    android:text="@string/alarm_text1" /> 

<TextView 
    android:id="@+id/lblAlarmTime" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:layout_below="@id/alarm_picker_text1" 
    android:text="" /> 

<TextView 
    android:id="@+id/alarm_picker_text2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center" 
    android:layout_below="@id/lblAlarmTime"   
    android:text="@string/alarm_text2" /> 

<Button 
    android:id="@+id/btn_set_time" 
    android:layout_width="100dp" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/alarm_picker_text2" 
    android:layout_alignParentLeft="true" 
    android:text="@string/btn_set_time" /> 

<ToggleButton 
    android:id="@+id/togglebutton" 
    android:layout_width="100dp" 
    android:layout_height="wrap_content" 
    android:textOn="On" 
    android:textOff="Off" 
    android:layout_below="@id/alarm_picker_text2" 
    android:layout_toRightOf="@id/btn_set_time" 
    android:onClick="onToggleClicked"/> 

<Button 
    android:id="@+id/btn_clock" 
    android:layout_width="140dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:onClick="displayClock" 
    android:text="@string/clock" /> 

<Button 
    android:id="@+id/btn_setAlarm" 
    android:layout_width="140dp" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_toRightOf="@id/btn_clock" 
    android:text="@string/set_alarm"/> 

<TimePicker 
    android:id="@+id/timePicker1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" /> 

</RelativeLayout> 

下面是代碼:

public class SetAlarmActivity extends FragmentActivity{ 

private TimePicker timePicker1; 

private Button btnSetAlarm; 
private TextView tvDisplayAlarmTime; 

static final int TIME_DIALOG_ID = 999; 

@SuppressLint("NewApi") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_set_alarm); 

    // Make sure we're running on Honeycomb or higher to use ActionBar APIs 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     // Show the Up button in the action bar. 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
    }       
} 

public Dialog onCreateDialog(Bundle savedInstanceState) { 

// Use the current time as the default values for the picker 
final Calendar c = Calendar.getInstance(); 
int hour = c.get(Calendar.HOUR_OF_DAY); 
int minute = c.get(Calendar.MINUTE); 


addListenerOnButton(); 

TextView tvDisplayAlarmTime = (TextView) findViewById(R.id.lblAlarmTime); 
// Display the time selected in the Text View 
tvDisplayAlarmTime.setText("test"+ hour + ":" + minute);   

// Create a new instance of TimePickerDialog and return it 
return new TimePickerDialog(this,timePickerListener, hour, minute, 
      DateFormat.is24HourFormat(this)); 
} 

    public void addListenerOnButton() { 

     final Button btnSetAlarm = (Button) findViewById(R.id.btn_set_time);   
     btnSetAlarm.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // Update the Time Set in the TextView 
       TextView tvDisplayAlarmTime = (TextView) findViewById(R.id.lblAlarmTime); 
       // Display the time selected in the Text View 
       tvDisplayAlarmTime.setText("test"); 
       //tvDisplayAlarmTime.setText("test"+ mHour + ":" + mMinute); 
       //showTimePickerDialog(tvDisplayAlarmTime); 

      } 
     }); 
    } 

    public void displayClock(View view) { 
    /* Called when the user clicks the Clock button */ 
     Intent intent = new Intent(this, MainActivity.class); 
     startActivity(intent); 
    } 

    // the callback received when the user "sets" the time in the dialog 
    private TimePickerDialog.OnTimeSetListener timePickerListener = 
     new TimePickerDialog.OnTimeSetListener() {    
     public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
       int mHour = hourOfDay; 
       int mMinute = minute; 

       TextView tvDisplayAlarmTime = (TextView) findViewById(R.id.lblAlarmTime); 
       // Display the time selected in the Text View 
       tvDisplayAlarmTime.setText("test"+ mHour + ":" + mMinute); 
       //showTimePickerDialog(tvDisplayAlarmTime); 
      } 
     };    

/** 
* Set up the {@link android.app.ActionBar}, if the API is available. 
*/ 
@TargetApi(Build.VERSION_CODES.HONEYCOMB) 
private void setupActionBar() { 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
    } 
} 


// Display Clock to set Alarm 
@SuppressLint("NewApi") 
public void showTimePickerDialog(View v) { 
    DialogFragment newFragment = new DialogFragment(); 
    newFragment.show(getFragmentManager(), "timePicker"); 
} 
}  

回答

1

定義在全球和在onCreate方法初始化的TextView,並用它在對話框中,因爲烏爾使用片段初始化它在onActivityCreated,

TextView setTimeText2; 
@Override 
public void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 

TextView setTimeText=(TextView) findViewById(R.id.setTime);} 



@Override 
public void onClick(View v) { 
    switch(v.getId()){ 

    case R.id.alarmTime: 
     openTimePickerDialog(false); 
     break; 

    } 
} 
public void openTimePickerDialog(Boolean is24r) { 
    timePickerDialog = new TimePickerDialog(
      SetAlarmCall.this, 
      onTimeSetListener, 
      calNow.get(Calendar.HOUR_OF_DAY), 
      calNow.get(Calendar.MINUTE), 
      is24r); 
    timePickerDialog.setTitle("Set Alarm Time"); 
    timePickerDialog.show(); 
    } 
OnTimeSetListener onTimeSetListener 
= new OnTimeSetListener(){ 
    @Override 
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) { 
     calSet.set(Calendar.HOUR_OF_DAY, hourOfDay); 
     calSet.set(Calendar.MINUTE, minute); 
     calSet.set(Calendar.SECOND, 0); 
     if(calSet.compareTo(calNow) <= 0){ 
      //Today Set time passed, count to tomorrow 
      calSet.add(Calendar.DATE, 1); 
     } 
     timeFlag=true; 
     sb=new StringBuilder(); 
     sb.append(calSet.get(Calendar.HOUR)+":"); 
     if(calSet.get(Calendar.MINUTE)<10) 
      { 
      sb.append("0"+calSet.get(Calendar.MINUTE)+" "); 
      } 
     else { 
      sb.append(calSet.get(Calendar.MINUTE)+" "); 
     } 

     if(calSet.get(Calendar.HOUR_OF_DAY)>=12){ 
      sb.append("PM"); 
     } 
     else{ 
      sb.append("AM"); 
     } 
     setTimeText2.setText(sb.toString()); 
    }}; 
+0

謝謝!它效果很好! – sowens

相關問題