我正在创建一个可以播放音乐的闹钟。我使用 TimePicker 显示设置闹钟的时间,并创建了一个带有侦听器的按钮(设置时间),以从 TimePicker 和按钮中捕获时间。我想在手机上使用 TextView (lblAlarmTime) 显示时间。我无法获取要在 TextView 中显示的值。我不知道我的听众是否不正确,或者我试图错误地显示文本。我想在“您的闹钟设置为:”标签下显示捕获的时间。
我是 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");
}
}