0

我正在创建一个可以播放音乐的闹钟。我使用 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");
}
}    
4

1 回答 1

1

在全局中定义并在 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());
    }};
于 2013-03-29T04:53:43.627 回答