音楽が鳴る目覚まし時計を作っています。TimePicker を使用してアラームを設定する時間を表示し、TimePicker とボタンから時間をキャプチャするリスナーを含むボタン (Set Time) を作成しました。電話で TextView (lblAlarmTime) を使用して時刻を表示したい。TextView に表示する値を取得できません。リスナーが間違っているのか、それともテキストを正しく表示しようとしているのかわからない。「Your Alarm is Set For:」というラベルの下にキャプチャされた時間を表示したいと思います。
私は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");
}
}