Android 개발 시간 날짜 구성 요소 사용법 인 스 턴 스

이전 시간 과 날짜 설정 의 예제 에 이 어 오늘 은 안 드 로 이 드 의 레이아웃 구성 요소 에 시간 과 날짜 에 대한 설정 이 있 는 구성 요 소 를 소개 합 니 다.도움 이 되 기 를 바 랍 니 다.구체 적 으로 다음 과 같다.
시간 날짜 설정 구성 요소:TimePicker,DatePicker
레이아웃 파일 에 레이아웃 스타일 에 직접 추가 할 수 있 습 니 다.구체 적 인 코드 는 다음 과 같 습 니 다.

<LinearLayout 
    android:id="@+id/linear1"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
  <EditText
    android:id="@+id/editview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
  <LinearLayout 
    android:id="@+id/linear2"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ><br>    //      
    <TimePicker 
      android:id="@+id/time"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      />  <br>    //      
    <DatePicker
      android:id="@+id/date"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      />
  </LinearLayout>
</LinearLayout>

.java 파일 코드 는 다음 과 같 습 니 다.

public class MainActivity extends Activity {
  private EditText myedit = null;<br>    //         
  private TimePicker time = null;
  private DatePicker date = null;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);<br><br>         //  Activity        
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    myedit = (EditText)findViewById(R.id.editview);<br>         //            
    time = (TimePicker)findViewById(R.id.time);
    date = (DatePicker)findViewById(R.id.date);
    //time.setIs24HourView(true);//    24        ,    12   
    time.setOnTimeChangedListener(new mytime());<br>         //             ondatechangedlistener()  ,           init()    。
    date.init(this.date.getYear(), this.date.getMonth(), this.date.getDayOfMonth(), new myDate());
    this.setDateTime();
  }
      //        :    <br><br>      class mytime implements OnTimeChangedListener{
    @Override
    public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
      // TODO Auto-generated method stub
      MainActivity.this.setDateTime();
    }    
  }
  <br>    //        
  @SuppressLint("NewApi")
  class myDate implements OnDateChangedListener{
    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
      // TODO Auto-generated method stub
      MainActivity.this.setDateTime();
    }    
  }
  public void setDateTime() { //                        
    this.myedit.setText(this.date.getYear() + "-"
        + (this.date.getMonth() + 1) + "-" + this.date.getDayOfMonth()
        + " " + this.time.getCurrentHour() + ":"
        + this.time.getCurrentMinute()); //        
  }
}
효과 도 는 다음 과 같다.
 
이 예 에서 비교적 어 려 운 점 은 날짜 이벤트 에 OnChanged Listener()방법 이 없다 는 것 이다.여기 서 우 리 는 날짜 의 init()방법 으로 대체 했다.그 다음은 Activity 의 디 스 플레이 방향 을 어떻게 제어 하 는 지 입 니 다.Activity 의 디 스 플레이 방향 은 기본적으로 세로 화면 으로 표 시 됩 니 다.우 리 는 자바 파일 을 통 해 새로운 설정 을 할 수도 있 고 설정 파일 에서 설정 할 수도 있 습 니 다.이것 도 자바 파일 에서 비교적 유연 하 게 설정 할 수 있 습 니 다.

좋은 웹페이지 즐겨찾기