Android 개발 시간 날짜 작업 실례

핸드폰 의 시간 과 날짜 설정 에 대해 모두 가 낯 설 지 않 을 것 이 라 고 믿 습 니 다.오늘 은 시간 과 날짜 설정 에 관 한 예 를 들 어 그 중에서 약간 완선 되 지 않 은 점 이 있 습 니 다.예 를 들 어 설정 한 시간 과 날짜,핸드폰 시스템 을 어떻게 동기 화 하 는 지 등 입 니 다.관심 있 는 독 자 는 자신의 경험 에 따라 보완 할 수 있다.
이제 구체 적 인 예 시 를 살 펴 보 겠 습 니 다.여러분 에 게 도움 이 되 기 를 바 랍 니 다.
우선 시간 설정:
.java 파일(MainActivity.java)코드 는 다음 과 같 습 니 다.

package com.example.activity_time_date;
import java.util.Calendar;
import android.app.Activity;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.TimePicker;
public class MainActivity extends Activity {
 private TextView mytext = null;
 private Button mybutton1 = null;
 private Button mybutton2 = null;
 private int mHour;
 private int mMinute;
 static final int TIME_DIALOG_ID = 0;
 private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
  @Override
  public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
   // TODO Auto-generated method stub
   mHour = hourOfDay;
   mMinute = minute;
   updateDisplay();
  }
 };
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  mytext = (TextView)findViewById(R.id.textview);
  mybutton1 = (Button)findViewById(R.id.button1);
  mybutton2 = (Button)findViewById(R.id.button2); 
  mybutton1.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    showDialog(TIME_DIALOG_ID);
   }
  });
  mybutton2.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    MainActivity.this.startActivity(new Intent(MainActivity.this, dateActivity.class));
   }
  });
  final Calendar cal = Calendar.getInstance();
  mHour = cal.get(Calendar.HOUR_OF_DAY);
  mMinute = cal.get(Calendar.MINUTE);
  updateDisplay();
 }
 private void updateDisplay(){
  mytext.setText(new StringBuilder().append(pad(mHour)).append(":")
    .append(pad(mMinute)));
 }
 private static String pad(int i){
  if (i >= 10)
   return String.valueOf(i);
  else
   return "0" + String.valueOf(i);
 }
 @Override
 protected Dialog onCreateDialog(int id) {
  switch (id) {
  case TIME_DIALOG_ID:
   return new TimePickerDialog(this, mTimeSetListener, mHour, mMinute,
     false);
  }
  return null;
 }
}

레이아웃 파일(activitymain.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"
 tools:context="${relativePackage}.${activityClass}" >
<LinearLayout 
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="vertical"
 >
 <TextView
  android:id="@+id/textview"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  />
 <Button 
  android:id="@+id/button1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="    "
  />
 <Button 
  android:id="@+id/button2"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="   "
  />
</LinearLayout>
</RelativeLayout>
  
다음 그림 과 같이 실행 효과:

날짜 설정 과 시간 설정 이 기본적으로 일치 합 니 다.더 이상 언급 하지 않 겠 습 니 다.독자 들 은 본 논문 의 예시 코드 를 디 버 깅 하고 개선 할 수 있 으 며 새로운 수확 이 있 을 것 이 라 고 믿 습 니 다!

좋은 웹페이지 즐겨찾기