我正在制作一个播放音乐的闹钟。我使用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");
}
}
答案 0 :(得分:1)
在global中定义并在onCreate方法中初始化textview并在对话框中使用它,因为你使用Fragments在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());
}};