回答

1

這裏是與您的所有需求的功能性的例子。 測試和100%的功能:)

將這個在XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
> 


<TextView 
    android:id="@+id/timer_textview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Check this to Cancel Alarm" 
    android:layout_above="@+id/time_picker" 
    android:layout_toRightOf="@+id/time_picker_checkbox" 
    /> 

<CheckBox 
    android:id="@+id/time_picker_checkbox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/time_picker" 
    /> 

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

,這裏是你的Java代碼:

package com.example.chxboxtest; 

    import android.app.Activity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.CheckBox; 
    import android.widget.TimePicker; 
    import android.widget.Toast; 

    public class MainActivity extends Activity implements OnClickListener { 

private CheckBox cBox; 
private TimePicker tPicker; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    initButtons(); 

} 

private void initButtons() { 

    tPicker = (TimePicker) findViewById(R.id.time_picker); 

    cBox = (CheckBox) findViewById(R.id.time_picker_checkbox); 
    cBox.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 

    switch (v.getId()) { 

    case R.id.time_picker_checkbox: { 

     if (((CheckBox) v).isChecked()) { 

     Toast.makeText(this, "Deactivate",Toast.LENGTH_SHORT).show(); 
      tPicker.setEnabled(false); 
     } else { 
      Toast.makeText(this, "Activate",Toast.LENGTH_SHORT).show(); 
      tPicker.setEnabled(true); 
     } 

    } 

     break; 

    default: 
     break; 

    } 

} 

    } 

如果你想在一個自定義的這個例子對話請讓我知道。

乾杯,

+0

什麼是 「@捫/ activity_vertical_margin」? 謝謝! – elichai2

+0

當您創建部分將通過月食來創建一個新的項目,你可以忽略這部分:) 的android:paddingBottom來= 「@捫/ activity_vertical_margin」 的android:paddingLeft = 「@捫/ activity_horizo​​ntal_margin」 的android:paddingRight =「@ dimen/activity_horizo​​ntal_margin」 android:paddingTop =「@ dimen/activity_vertical_margin」 –

+0

好吧,我刪除了這些行(它給我錯誤)。 當我嘗試致電該活動時出現錯誤: http://pastebin.com/27RLUwFT (我通過以下方式調用它:'startActivity(new Intent(this,TimePickerTest)。類));')(我叫你''TimePickerTest') 謝謝! – elichai2

2

我會把這裏的整個項目還爲如果他們需要別人來使用它: 簡短說明:可以讓你獲得由用戶設定的時間的,並顯示在底部的程序在按下完成按鈕之後,用戶可以從一個活動獲取到另一個活動。

--------主要活動-------

package com.example.chxboxtest; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MainActivity extends Activity implements OnClickListener { 

private Button start_intent_button; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    start_intent_button = (Button) findViewById(R.id.start_intent_button); 
    start_intent_button.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 

    switch (v.getId()) { 

    case R.id.start_intent_button: { 

     Intent intent = new Intent(this,TimePickerTest.class); 
     startActivity(intent); 

    } 
     break; 
    } 

} 

} 

-------------主要活動XML ------ -----------

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
> 

<Button 
    android:id="@+id/start_intent_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Start Intent Time Picker" 
    android:layout_centerInParent="true" 
    /> 


</RelativeLayout> 

------------- TimePicker類---------------

package com.example.chxboxtest; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.TextView; 
import android.widget.TimePicker; 
import android.widget.Toast; 


public class TimePickerTest extends Activity implements OnClickListener{ 

private CheckBox cBox; 
private TimePicker tPicker; 
private TextView showTime; 
private Button done; 

private String hour; 
private String minute; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.time_picker_layout); 

    initButtons(); 
} 



private void initButtons() { 

    tPicker = (TimePicker) findViewById(R.id.time_picker); 
    showTime = (TextView) findViewById(R.id.get_time); 
    done = (Button)findViewById(R.id.done); 
    cBox = (CheckBox) findViewById(R.id.time_picker_checkbox); 
    cBox.setOnClickListener(this); 
    done.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 

    switch (v.getId()) { 

    //If check enable or disble timePicker 
    case R.id.time_picker_checkbox: { 

     if (((CheckBox) v).isChecked()) { 
      Toast.makeText(this, "CHECKED", Toast.LENGTH_SHORT).show(); 
      tPicker.setEnabled(false); 
     } else { 
      Toast.makeText(this, "NOT CHECKED", Toast.LENGTH_SHORT).show(); 
      tPicker.setEnabled(true); 
     } 
    } 
     break; 

//If Done button pressed get time selected by user 

    case R.id.done:{ 


     tPicker.clearFocus(); 
     // re-read the values, in my case i put them in a Time object. 
     hour = tPicker.getCurrentHour().toString(); 
     minute = tPicker.getCurrentMinute().toString(); 

     if(tPicker.getCurrentMinute().intValue() < 10){ 

      String setTimeText = hour+ " : " + "0" + minute; 
      showTime.setText(setTimeText); 
     }else{ 
      String setTimeText = hour+ " : " + minute; 
      showTime.setText(setTimeText); 
     } 





    } 
     break; 

    default: 
     break; 

    } 

} 

    } 

------------- TimerPicker XML ------------

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<TextView 
    android:id="@+id/timer_textview" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/time_picker" 
    android:layout_toRightOf="@+id/time_picker_checkbox" 
    android:text="Check this to Cancel Alarm" /> 

<CheckBox 
    android:id="@+id/time_picker_checkbox" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/time_picker" 
    android:layout_centerHorizontal="true" /> 

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

<Button 
    android:id="@+id/done" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/time_picker" 
    android:layout_centerHorizontal="true" 
    android:gravity="center" 
    android:text="Done" /> 

<TextView 
    android:id="@+id/get_time" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:gravity="center_horizontal" 
    android:text="Time" 
    android:textColor="#FF0000" 
    android:textSize="25sp" /> 

</RelativeLayout> 

------------- -----------的Manifest.xml

 **<activity 
     android:name="com.example.chxboxtest.TimePickerTest" 
     android:configChanges="orientation|keyboardHidden"> 
    </activity>** 

不要忘記添加TimePicker類的活動。 BestPracice添加所有XML文本中的strings.xml

乾杯

+0

對不起過去但是最後一件事: 你能告訴我怎樣才能把它添加到對話框中?謝謝! – elichai2

+0

當然,但我會從工作回家:) –

+0

你還記得我嗎? 謝謝! – elichai2